Jump to content
Sign in to follow this  
twitchyliquid64

Assignment and Copy Constructors in C++ Classes [SOLVED]

Recommended Posts

twitchyliquid64

Hi all,

Im building a variant class that I want to make work, but Im running into compiler issues; issues that highlight that I have no idea what is going on under the hood for operators in C++.

My class looks something like:

class Variant
{
public:
    //Constructors
    Variant(void) : type(VAR_INTTYPE), int64value(0), databuffsize(0), databuff((char*)0) {}
    Variant(int);
    Variant(long long);
    Variant(double);
    Variant(const char* in);
    Variant(char* in);
    Variant(Variant & in);//Copy Constructor
    ~Variant();//Destructor


    //Operators
    Variant& operator= (Variant&);
    Variant& operator= (Variant);
    //Other irrelvant code...
}

And Im trying to do things like:

Variant test;
int main()
{  
    test = Variant(20); //btw, I have no intention of implementing assignment ops for primitive types for various reasons
    Variant lol(7);
    lol = test;
    return 0;
}

In GCC (mingw) this is throwing errors like:

error: no matching function to call for 'Variant::Variant(Variant)'

Any ideas what constructors or operators I am missing? I have looked around and cannot find any solution to my problem.

Thanks in advance.

Edited by twitchyliquid64

ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Share this post


Link to post
Share on other sites
Valik

Copy constructor is not const correct. Also, did you actually implement the copy constructor or just declare it? If you did declare it in a separate file you didn't make it inline, did you?

Share this post


Link to post
Share on other sites
twitchyliquid64

Copy constructor is not const correct. Also, did you actually implement the copy constructor or just declare it? If you did declare it in a separate file you didn't make it inline, did you?

all the methods listed there are fully implemented in the variant.cpp file.

I'm pretty sure i inlined it ... is that bad? file size isn't an issue.

what do you mean by not const correct? do you mean i need a constructor with a const variant reference as the parameter?


ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Share this post


Link to post
Share on other sites
Richard Robertson

Copy constructor is name(const &name)

Share this post


Link to post
Share on other sites
Kip

Instead of overloading the assigment operator twice:

Variant& operator= (Variant&);
    Variant& operator= (Variant);

I would do this:

Variant const& operator=(const Variant&);

Share this post


Link to post
Share on other sites
Richard Robertson

Why would you force a const return? What if I wanted to do something like (a = B).change()?

Edited by Richard Robertson

Share this post


Link to post
Share on other sites
Valik

If you inline a function in a CPP file then it cannot be seen outside that CPP file. It must either be inlined in the header file or you must remove the inline flag in the CPP file.

Share this post


Link to post
Share on other sites
twitchyliquid64

Thanks everyone for your help. In the end the problem was that my copy constructor needed to accept a const reference rather than just a reference.


ongoing projects:-firestorm: Largescale P2P Social NetworkCompleted Autoit Programs/Scripts: Variable Pickler | Networked Streaming Audio (in pure autoIT) | firenet p2p web messenger | Proxy Checker | Dynamic Execute() Code Generator | P2P UDF | Graph Theory Proof of Concept - Breadth First search

Share this post


Link to post
Share on other sites
Richard Robertson

Copy constructor is name(const &name)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • nacerbaaziz
      By nacerbaaziz
      Hi dear
      I have a question please
      Can we customize error messages in autoit script?
      I mean internal error messages
      for example
      AutoIt Error
      Line 4  (File "D:\my projects\NVDA Manager\New AutoIt v3
      Script.au3"):
      $script[1] = 1
      $script^ ERROR
      Error: Subscript used on non-accessible variable.
      For example, I want to customize this message
      Is this possible?
      am waiting for your answers
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to make a program that can look at a folder directory, find all the CSV files, and then add the data from CSV's to an array. 
      The problem I seem to be running into is on the 2nd iteration (2nd file) when the script will not create an array. Could someone please help? Thanks in advance
       
      #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> #include <Excel.au3> #include <MsgBoxConstants.au3> Global $MasterArray RefineData() Func RefineData() Local $i, $filenum, $file, $csvArray, $sFilePath = @ScriptDir $fileList = _FileListToArrayRec($sFilePath, "*.csv", 1) ;Create and array of all .csv files within folder ;=====Loop through the .csv files within the folder====== For $filenum = 1 To UBound($fileList) - 1 Step 1 $file = $fileList[$filenum] $sFilePath = $sFilePath & "\" & $file ;=====Create array based on csv file===== _FileReadToArray($sFilePath, $csvArray, $FRTA_NOCOUNT, ",") _ArrayDisplay($csvArray,"File: " & $filenum) If $filenum = 1 Then $MasterArray = $csvArray _ArrayDisplay($MasterArray, "Master") Else $MasterArray = _ArrayColInsert($MasterArray, UBound($MasterArray)) ;want column added at end For $i = 0 To UBound($MasterArray)-1 Step 1 $MasterArray[$i][UBound($MasterArray) - 1] = $csvArray[$i][4] Next _ArrayDisplay($MasterArray, "Master") EndIf Next EndFunc ;==>RefineData  
    • ur
      By ur
      In the autoit file, if we add multiple libraries (both internal and custom created one)
       
      When we compile the code and run the exe, some time we are getting runtime errors.
      But the line number of error is not showing the exact line number, might be it is including the line number count of libraries also.
      Can you please suggest how to get the exact error line number.
      So, it will be useful for debugging.
    • AndyS19
      By AndyS19
      I want to copy just the selected text in an input control and put it to the clipboard, but GuiCtrlRead() always reads all of the text in the control.
      In this test code, when I highlight a  portion of the left control's text and press ^C, I want to copy just that text to the clipboard; but when I highlight a  portion of the right control's text and press ^C, I want to copy all of its text to the clipboard.
      #include <Debug.au3> _DebugSetup(@ScriptName & "_debug.txt", False, 2, "") _DebugOut("=============== " & @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & " ====================") #include <GUIConstants.au3> #include <WinAPI.au3> #include <Array.au3> Opt("GUICloseOnESC", 1) ; ESC closes GUI? (0 = no, 1 = yes) Opt("GUIOnEventMode", 1) ; Set OnEvent mode Opt('MustDeclareVars', 1) OnAutoItExitRegister("Event_GUIClose") Opt("GUIEventOptions", 1) ;0=default, 1=just notification, 2=GuiCtrlRead tab index Opt("WinTitleMatchMode", -2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase Global $hWnd_MainWin, $iID_Left, $iID_Right _Main() Func _Main() $hWnd_MainWin = GUICreate("test7", 200, 300) $iID_Left = GUICtrlCreateInput("AAA BBB CCC (get Selected text)", 10, 30, 200, 25) GUICtrlSetOnEvent($iID_Left, "handle_left_btn") $iID_Right = GUICtrlCreateInput("DDD EEE FFF (get All text)", 10, 60, 200, 25) GUICtrlSetOnEvent($iID_Right, "handle_right_btn") Local $aAccelKeys[1][2] = [["^c", $hWnd_MainWin]] GUISetAccelerators($aAccelKeys) GUISetOnEvent($GUI_EVENT_CLOSE, 'Event_GUIClose') GUISetState() ; Make everything visible While 1 Sleep(127) WEnd EndFunc ;==>_Main Func handle_CTRL_C_key() ConsoleWrite("+++: handle_CTRL_C_key() entered" & @CRLF) Local $hCtrl, $ctrlID, $control_classname, $text $control_classname = ControlGetFocus($hWnd_MainWin) $hCtrl = ControlGetHandle($hWnd_MainWin, "", $control_classname) $ctrlID = _WinAPI_GetDlgCtrlID($hCtrl) If ($ctrlID = $iID_Left) Then $text = GUICtrlRead($ctrlID) ClipPut($text) ConsoleWrite("+++: Copied selected text:" & @CRLF & "==>" & $text & "<==" & @CRLF) ElseIf ($ctrlID = $iID_Right) Then $text = GUICtrlRead($ctrlID) ClipPut($text) ConsoleWrite("+++: Copied all text:" & @CRLF & "==>" & $text & "<==" & @CRLF) EndIf EndFunc ;==>handle_CTRL_C_key Func Event_GUIClose() Exit (99) EndFunc ;==>Event_GUIClose Func handle_left_btn() ConsoleWrite("+++: handle_left_btn() entered" & @CRLF) EndFunc ;==>handle_left_btn Func handle_right_btn() ConsoleWrite("+++: handle_right_btn() entered" & @CRLF) EndFunc ;==>handle_right_btn
       
    • Rskm
      By Rskm
      i have seen many pointing out issues related to this, i tried reading through them - still unable to solve mine.. i have a script which reads certain lines based on a search item, this is an iterative process - the ldflst_file gets updated for each iteration and the script finds the line number which has the search item. this works perfectly for few iterations, but stops in some cases abruptly with the error = 
      Subscript used on non-accessible variable $aRead^ ERROR I have verified that the search item is present in the file. what could be the reason for this code to stop in certain cases. thanks for help
      my code below (partial).. full code is in attachment
       
      local $aRead=FileReadToArray($ldflst_file)
      global $ldflst_var1=" JOINT  LOAD       FORCE(X)        FORCE(Y)        FORCE(Z)       MOMENT(X)       MOMENT(Y)       MOMENT(Z)"
       
      for $i= 0 to $ldflst_totlines    ; ldflst_totlines is total number of  lines in the file 'ldflst_file'

          if $aRead[$i]=$ldflst_var1 Then
              $ldflst_LFound=$i
              ExitLoop
              else
              EndIf
      Next
       
       
      nomo_new1.au3
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.