2012-06-08 46 views
0

在Abc.hpp文件中的下列信息被定義:如何寫賦值=操作

class Abc: public A 
{ 
enum Ac { VAR }; 

    struct L{ 
     std::string s1; 
     ::Class2::ID type; 
     unsigned s; 
     bool operator==(const L& l) const { return (type==l.type)&&(s==l.s)&&(s==l.s); } 
    }; 

    class SS 
    { 
     public: 
     virtual ~SS(); 
    }; 
    class IS { 
     public: 
     /// Destructor 
     virtual ~IS(); 

    }; 

    class HashIndexImplementation; 
    class HashIndex; 

void func(){} 

    Abc& operator=(Abc&) { 
    cout << "A::operator=(A&)" << endl; 
    return *this; 
    } //It gives me the error that the token '{' is not recognized 

Abc(Class2 & part); 
}; 

對於上述類目的在與另一類我的目的分配的以下信息:

Abc d; 
static Abc f; 
f=d; 

但是,我上面寫的代碼不起作用...它引發的錯誤是:

no matching function for call to Abc::Abc() 

編輯:我正在處理整個類的層次結構,因此如果我添加像Abc()這樣的另一個構造函數,那麼我將被迫在多達20個類中進行更改......是否沒有其他方法可用於分配。 是否有一些方法可以將其他構造函數合併到一起。

+1

好閱讀:http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom –

+0

您的abc類聲明最後缺少';'。 – juanchopanza

+0

這是功課嗎?如果是的話,那很好,我們只是在回答之前知道。 –

回答

3
no matching function for call to Abc::Abc() 

您需要提供一個構造函數,如果你想要實例化類對象,它不帶任何參數爲:

Abc d; 

這是因爲編譯器不會生成默認的無參數的構造函數,如果你提供任何你自己的構造函數。您提供了自己的拷貝構造函數,因此編譯器強制您提供自己的無參數構造函數。

+0

我正在處理整個類的層次結構,因此如果我添加另一個構造函數(如Abc()),那麼我將被迫在多達20個類中進行更改......是否沒有其他方法可用於分配。是否有一些方法可以將其他構造函數合併到一起。 –

0

Abc的右大括號後沒有分號。嘗試添加分號,它應該解決問題。

0

在已經剝離出來的一切無關緊要,生產short self-contained compilable example(請做到這一點自己未來的問題),我想出了這一點:

#include <iostream> 
#include <iomanip> 
#include <map> 
#include <string> 
using namespace std; 
class Abc 
{ 
enum Ac { VAR }; 

    Abc& operator=(Abc&) { 
    cout << "A::operator=(A&)" << endl; 
    return *this; 
    } //It gives me the error that the token '{' is not recognized 

}; 

int main() 
{ 
    Abc abc; 
} 

這編譯,但並沒有真正做多。您對入境Abc參考不做任何處理。就語言語法而言,技術上是正確的,但沒有任何有用的工作。通常你會做沿着此線的東西:

class Abc 
{ 
    int foo_; 
    Abc& operator=(Abc& rhs) { 
    cout << "A::operator=(A&)" << endl; 

    foo_ = rhs.foo_; 

    return *this; 
    } //It gives me the error that the token '{' is not recognized 
}; 

除此之外,你有語法錯誤,現在已經消失。有很多未定義的東西,如Abc的基類,B::class2。也許你需要#include來引入這些定義。