user429400
user429400

Reputation: 3325

operator overloading

I've created a class myString and I'm trying to run the following code:

class myString{
    char* str;
    int len;

public:
    myString(char* str1 = " "){
        len = strlen(str1);
        str = new char[len+1];
        strcpy(str, str1);
    };

    int getLen() const {
        return len;
    };

    char* getString() const {
        return str;
    };

    ~myString(){
        delete[] str;
    };

    myString& operator=(myString& orig){
        cout << "hello";
        if (str == NULL){
            delete[] str;
        };
        str = new char[orig.getLen()];
        strcpy(str, orig.getString());
        cout << this << endl;
        return *this;
    };

    ...
};



int main(){


    myString s("bla");
    myString k("dingo");
        myString g = s;
        // s=k;  //When this line is commented I get a linking error
         ...
   };

My Questions:

  1. Why "hello" does not get printed?
  2. Why the line s=k causes a linker error?

This is the error:

LINK : c:\users\perry\documents\visual studio 2010\Projects\inheritance\Debug\inheritance.exe not found or not built by the last incremental link; performing full link 1>main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream

&,class myString *)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@PAVmyString@@@Z) referenced in function "public: class myString & __thiscall myString::operator=(class myString &)" (??4myString@@QAEAAV0@AAV0@@Z) 1>c:\users\perry\documents\visual studio 2010\Projects\inheritance\Debug\inheritance.exe : fatal error LNK1120: 1 unresolved externals

Thanks, Li

Upvotes: 1

Views: 514

Answers (5)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506847

For the following, you need a copy constructor. It does not use the assignment operator function.

myString g = s;

Edit

For the linker error - I can't help you with that. Rather than guessing at what operator<< that line calls, I will just give up on this because you haven't provided the code. In normal C++ code, there is no way that a simple cout << this; would cause a linker error. cout has an operator that accepts a void const* for this. You have declared an operator somewhere that provides a better match but did not define it.

Upvotes: 3

David Thornley
David Thornley

Reputation: 57036

In the first place, myString g = s; is the initialization of a new myString, and that's done with the copy constructor (which is automatically provided because you didn't write one).

The link error is because of the line cout << this << endl;, since you haven't told the system how to print this, and it apparently can't come up with a way.

In C++, there's the Rule of Three: if you write a copy constructor, assignment operator, or destructor, you probably need to write all three. (The exception is writing a virtual destructor for a base class.) Typically, then, your class manages some resource (str in your case), and you need to handle it in all three cases.

Also, use const when you can. You can pass a non-const variable into a const reference, but not vice versa.

Your destructor is fine. Your assignment operator should have the signature myString & operator=(const &myString), and your copy constructor should have the signature myString::myString(const &myString).

Upvotes: 0

KeatsPeeks
KeatsPeeks

Reputation: 19327

myString g = s;

This doesn't call operator=, but the copy constructor. Since you have not defined any copy constructor, a default one is generated, copying all fields. But you're holding pointers, you don't want that to happen ! That's why you need a copy constructor.

About the linking error:

cout << this << endl;

Here, you try to use operator<< with this. The type of this is myString*, for which this operator is not defined. If you want to print the value of the this pointer, you can convert it:

cout << (void*) this << endl;



 if (str == NULL){
                delete[] str;
            };

You want if (str != NULL) here (typo ?).

Upvotes: 0

Greg Domjan
Greg Domjan

Reputation: 14095

Link error is that you have not defined

operator<<(class std::basic_ostream< >  &,class myString *)

due to the line

cout << this << endl;

Upvotes: 1

pm100
pm100

Reputation: 50110

I think it should be

operator=(const myString &orig)

Upvotes: 0

Related Questions