开发者

ambiguous copy constructors vc 2008

开发者 https://www.devze.com 2022-12-15 14:56 出处:网络
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

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 :-)

clas开发者_如何学JAVAs 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号