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

    • rm4453
      By rm4453
      Hello,
       
      I am currently writing a program that parses a massive table from a website, and need a way to add a progress bar while parsing.
      I am currently using the function _IETableWriteToArray($oObj, True) to parse the array. I need the progress bar to update as the table is parsed, not just at the end of the parsing.
      Any help at all would be very much appreciated!
       
      *EDIT --> The array I am left with after parsing is $array[0-50000][16]
    • xiantez
      By xiantez
      This script used to work on an older version of AutoIT. Currently I am running AutoIT v3.3.14.5 and it's failing.
      Func PublicIP() ;Post public facing IP address Local $url = 'https://www.google.com/search?client=opera&q=what+is+my+ip&sourceid=opera&ie=UTF-8&oe=UTF-8' Local $getIPaddress = BinaryToString(InetRead($url)) Local $sStart = 'clamp:2">' Local $sEnd = '</div>' Local $ipaddress = _StringBetween($getIPaddress, $sStart, $sEnd For $i In $ipaddress MsgBox(0, 'External IP', "Your public IP address is " & $i) Next EndFunc ;==>PublicIP The console output shows:
      "C:\Users\user\Documents\AutoIT\Scripts\WSI Tools.au3" (197) : ==> Variable must be of type "Object".: For $i In $ipaddress For $i In $ipaddress^ ERROR ->14:12:16 AutoIt3.exe ended.rc:1 +>14:12:16 AutoIt3Wrapper Finished. >Exit code: 1 Time: 9.811
    • Xandy
      By Xandy
      I'm listing the error message strings, and roughly matching the error code
      Is this how to do error reporting?  Is there a more simple way I don't know about?
      I realise I might not need to trap @error inside $error.  I like it this way b/c I have no fear of overwriting @error now; I don't even use it.
      Func Send_Connect($address, $port)     $socket = TCPConnect($address, $port)     $error = @error     If $error <> 0 Then         Local $aError[4] = ["-2 not connected.", _                             "1 IPAddr is incorrect.", _                             "2 port is incorrect."]         $aError[3] = "10060 Connection timed out."         If $error < 0 Then $error = 0         If $error > 2 Then $error = 3         MsgBox(0, "TCP Connect Error", $aError[$error], 0)         $socket = 0     EndIf     Return $socket EndFunc
    • Schneeflocke
      By Schneeflocke
      #RequireAdmin
      #include <NomadMemory.au3>
      $Value = _Pointer()
      MsgBox(0,"", "Value: " = $Value)
      Func _Pointer()
      Global $iBase = 0x7FF649DB0000, $iStaticofsset = 0x01559C08
      Global $hOpen, $sRead
      $hOpen = _MemoryOpen(ProcessExists("Tutorial-i386.exe"))
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x70
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0xA0
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x20
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x1B0
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x40
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x10
      $sRead = _Memoryread($iBase + $iStaticoffset, $hOpen, 'Float') + 0x368
      $sValue = _Memoryread($sRead, $hOpen, 'Float')
      _MemoryClose($hOpen)
      Return $sValue
      EndFunc
      I Would like to create an Round Timer and Kers BoT for Assetto Corsa.
      Intelliboost need too many Performance
    • Dragonfighter
      By Dragonfighter
      Autoit version: 3.3.14.5
      System: Windows 10 Home x64
      C++ IDE: Code::Blocks 17.12
      When I call the Dll that I wrote it give me how return value -1073741819 and exit value 3221225477, I tried changing variables type but it didn't work.
      This is the Autoit code:
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=n #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** $dll = DllOpen(@ScriptDir & "\Test.dll") DllCall($dll, "NONE", "TestFunction", "STR", "1 string", "STR", "2 string", "STR", "3 string", "INT", 1) ;Here crash and doesn't show the MsgBox MsgBox(0, "", @error) DllClose($dll) This is the main.cpp code:
      #include <iostream> #include <Windows.h> #include "main.h" using namespace std; extern "C" { DECLDIR void TestFunction(std::string string1, std::string string2, std::string string3, int number1) { std::cout << string1 << std::endl; std::cout << string2 << std::endl; std::cout << string3 << std::endl; std::cout << number1 << std::endl; } } And that is the main.h:
      #ifndef _DLLTEST_H_ #define _DLLTEST_H_ #define DLL_EXPORT #if defined DLL_EXPORT #define DECLDIR __declspec(dllexport) #else #define DECLDIR __declspec(dllimport) #endif extern "C" { DECLDIR void TestFunction(std::string string1, std::string string2, std::string string3, int number1); } #endif  
      And the values of the variables that write to the SciTe console ae completely different.
      Here I attach the console output of the SciTe editor:
      >"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\DragonFighter\Desktop\Dll test.au3" /UserParams +>10:30:08 Starting AutoIt3Wrapper v.17.224.935.0 SciTE v.3.7.3.0 Keyboard:00000410 OS:WIN_10/ CPU:X64 OS:X64 Environment(Language:0410) CodePage:0 utf8.auto.check:4 +> SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE UserDir => C:\Users\DragonFighter\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper SCITE_USERHOME => C:\Users\DragonFighter\AppData\Local\AutoIt v3\SciTE >Running AU3Check (3.3.14.5) from:C:\Program Files (x86)\AutoIt3 input:C:\Users\DragonFighter\Desktop\Dll test.au3 +>10:30:08 AU3Check ended.rc:0 >Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\DragonFighter\Desktop\Dll test.au3" --> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop �Á+���Uø‰q�‰A�‹O`ÆEÿ�èèÖÿÿƒÄ €}ÿ!>10:30:10 AutoIt3.exe ended.rc:-1073741819 +>10:30:10 AutoIt3Wrapper Finished. >Exit code: 3221225477 Time: 3.414  
      Thanks in advance for every reply.
×