2009-12-30 77 views
0

我試圖重新編譯最新的Visual Studio(2008)中的舊代碼,以前的工作代碼現在無法編譯。其中一個問題是由於我班的操作員負擔過重。下面有簡化課來演示問題。如果我刪除int和char *的鑄造操作符,那麼它工作正常。所以解決我的問題的方法之一是將它們替換爲to_char和to_int,然後使用它們,但它需要對代碼進行大量更改(該類被大量使用)。必須有一些更好,更聰明的方法來解決它。任何幫助是極大的讚賞:-)模糊複製構造函數vc 2008

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 

回答

5

嘗試改變

test(test &source); 

test(const test &source); 

的問題是,test_proc調用返回臨時測試對象,它可以傳遞給一個接受const引用但不是純引用的函數。

+0

非常感謝!這種簡單的解決方案。你救了我的一天:-) – 2009-12-30 03:47:08