Sign in to follow this  
Followers 0
twitchyliquid64

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

9 posts in this topic

#1 ·  Posted (edited)

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



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

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

Copy constructor is name(const &name)

Share this post


Link to post
Share on other sites

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

#6 ·  Posted (edited)

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

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

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

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  
Followers 0

  • Similar Content

    • DagSa
      By DagSa
      Try to find a solution for help files to my autoit executable.
      Its installed from simple XP-POS cash computers to desktop with windows 7, 8, 10.
      The compiled AutoIt program itself work everywhere, but the help file??
      I need a help file called from from the program and I don't really know what working best in all OS platforms.
      1. A wordfile is helpful enough, but word is not installed everywhere.
      2. RTF is very big files with pictures.
      3. I could save it as pdf, but not all have it either.
      4. Tried  .MHT file, but downside is browser could cover hole screen if browser closed this way. I have not found any way to resize it to specific size when open up.
      5. Use of IE.au3 lib. Well it could open mht files ,but always got question about use it as default browser or not. Think this also make IE browser lose control so I could not resize.
      6. Look at making a  .CHM file, but I don't think its the future do use that format. (.hlp is no no and old)

      Any tips of do it in autoit or make a call to open up in some nice format working everywhere.
       
    • au3leecher
      By au3leecher
      Hellow everybody,
      I tryed to manipulate a game called "Assault Cube" which is a easy beginning in coding. I run the code but the wished value doesn't change, i don't understand why.
      The Pointers are fine, I checked that the pointers don't change every time while restart the game.
      My Code:
      <snip>
      _____________________________________________________________________________________________________________________________________________________________________________________________
      Checklist:
      The Offsets are 18C & 4
      In Cheat Engine: ("ac_client.exe"+0010F418) <- Pointer
      4 Bytes = 'dword'
       
    • geekyworld
      By geekyworld
      Hi. I want to read the help file on my Kindle ebook reader (to protect my eyes). Is it available in Kindle or epub format? I tried converting it myself but the results were not very good.
      --Thanks!
    • argumentum
      By argumentum
      UDF to intercept the error window of AutoIt, showing more details about the error, including ability to save and send by email!
    • Nareshm
      By Nareshm
      Hi All,
      I have excel file like this
      and i want to cut cell/text from excel to other software.

       
      I have to cut the cell of B column one by one and past into other software
      If Winexists("No Data Found")
      then restore cuted cell and goto next/down side cell
      How to do it ?