tags:

views:

41

answers:

1

hello I'm trying to recompile older code in latest Visual Studio (2008) and code that worked previously now fails to compile. One of the problems is due to overloaded operators for my class. below there is simplified class to demonstrate the problem. If I remove casting operators for int and char* then it works fine. So one of the ways to fix my issue is to replace them with procedures to_char and to_int and use them instead but it will require a lot of changes in the code (that class is heavily used). There must be some better, smarter way to fix it. any help is greatly appreciated :-)

class test
{
public:
    test();
    test(char* s2);
    test(int num);
    test(test &source);

    ~test();

    operator char*();
    operator int();

};

test::test()    {    
}

test::test(char* s2)    {
}

test::test(int num)    {
}

test::test(test &source)    {
}



test::operator char*()    {
}

test::operator int()    {
}

test test_proc()    {
    test aa;
    return aa;
}

int test_proc2(test aa)
{

return 0;
}

int main()
{
    test_proc2(test_proc());
}


//test.cpp(60) : error C2664: 'test_proc2' : cannot convert parameter 1 from 'test' to 'test'
//        Cannot copy construct class 'test' due to ambiguous copy constructors or no available copy constructor
+4  A: 

Try changing

  test(test &source);

to

  test(const test &source);

The issue is that the test_proc call returns a temporary test object, which can be passed to a function that accepts a const reference, but not a plain reference.

David Norman
thanks a lot! such a simple solution. you saved my day :-)
pawel olas