This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Assignment Operator Bug

I have compiled this code in MS Visual C++ Express 2008 and it works as it should be but when i compile this code in Mingw as a part of GCC ver 4.4.1-2 the input() function should return a temporary object to 'ob' object and invoke the assignment operator '=' but it doesn't and it shows me error :"no match for 'operator=' in 'ob=input()()' "

code: #include<iostream> #include<conio.h> #include<cstdio> #include<cstring>

using namespace std;

class sample{

    char *s;
        sample(){s=new char('\0');}
        sample(const sample &ob);
        ~sample(){if(s) delete []s; cout<<"Freeing\n";}
        void show(){cout<<s<<endl;}
        void set(char *str);
        sample operator=(sample &ob);


sample::sample(const sample &ob)
    s=new char[strlen(ob.s)+1];

void sample::set(char *str)
    s=new char[strlen(str)+1];

sample input()
    char instr[20];
    sample str;

    cout<<"Enter a string :";

    return str;


sample sample::operator=(sample &ob)
    /* If the target memory is not large enough
    then allocate new memory. */
if(strlen(ob.s) > strlen(s))
        delete []s;
        s = new char[strlen(ob.s)+1];
    strcpy(s, ob.s);
    return *this;

int main() { sample ob;

    ob=input(); //showing errors;

    return 0;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]