Jump to content

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


twitchyliquid64
 Share

Recommended Posts

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

Link to comment
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

Link to comment
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

Link to comment
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
 Share

×
×
  • Create New...