#include <iostream>
using std::cout;
using std::endl;

class aClass
{
    public:
        aClass()
        {
            this->aString = new char [1];;
            this->aString[0] = '\0';
            this->length = 0;
        }
        ~aClass()
        {
            cout << "Destructor" << endl;
            delete [] this->aString;
        }
        aClass(const char *);
        const aClass& aClass::operator=(aClass &);
    private:
        int length;
        char * aString;
};

aClass::aClass(const char * text): length (strlen(text))
{
    cout << "Conversion constructor " << endl;
    cout << "\ttext is '" << text << "'" << endl;

    this->aString = new char [this->length + 1];
    strcpy(this->aString, text);

}
const aClass & aClass::operator=(aClass & right)
{
    cout << "In Overloaded = operator" << endl;

    if (&right != this)
    {
        delete [] this->aString;
        this->aString = new char [right.length + 1];
        strcpy(this->aString, right.aString);
    }

    return *this;
}

int main()
{
    aClass aObj("string");

    aObj = "string2";

    return 0;

}

From what I read, what should happen with what I've written is the string "string2" should be converted to a temporary "aClass" Object using the conversion constructor.

This aClass object should then be passed to the overloaded operator= in aObj to do the assignment.

Instead I'm getting the following error

file.cpp: In function `int main()':
file.cpp:53: no match for `aClass& = const char[8]' operator
file.cpp:36: candidates are: const aClass& aClass::operator=(aClass&)

What have I missed?

aObj = "string2";

This line is wrong. The arguments of that assignment operator statement is a const char*. But you haven't defined a =operator that takes a constant char* as it's argument.

Declare something like this and it should be okay.

class aClass
{
    public:
        aClass()
        {
            this->aString = new char [1];
            this->aString[0] = '\0';
            this->length = 0;
        }
        ~aClass()
        {
            cout << "Destructor" << endl;
            delete [] this->aString;
        }
        aClass(const char *);
        const aClass& aClass::operator=(aClass &);
[B]        const aClass& aClass::operator=(const char *);
[/B]    private:
        int length;
        char * aString;
};

const aClass & aClass::operator=(const char* str)
{
    cout << "In Overloaded = operator for const char*" << endl;

    if ( this->aString )
        delete [] this->aString;
    length = strlen(str);
    this->aString = new char[ length + 1];
    strcpy(this->aString, str);
    return *this;
}

What I read said that I shouldn't need to overload operator= again if I have a conversion constructor like the one I have written.

It's suppose to create the temporary aClass Object using that and then give the overloaded operator= the temporary object to do the assignment.

The text has a long example that I haven't copied and pasted to test, but I don't see anything materially different from what I've written.

EDIT: what you wrote did work btw and thanks for the response.

It's resolved. operator= had to take a "const aClass &". I assume the temporary object created is const.

I know I don't particularly like when someone asks a question that they should have taken the time to find an answer for on their own. ie they end up finding out the answer on their own and appear not to have taken that time.

I thought I had and apologize if it comes across as though I hadn't.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.