2013-11-02 112 views
0
istream &operator>>(istream &in, iString &a) { 
     string name; 
     in>>name; 
     iString b(name); 
     delete a.chars; 
     a=b; 
     return in; 
} 
ostream &operator<<(ostream &out, const iString &a) { 
     out<<a.chars<<endl; 
     return out; 
} 

這些是我的結構寫的運營商:運營商<< , >>重載失敗特定類

error: no match for ‘operator>>’ in ‘std::cin >> * a[(((int)which) + -0x00000000000000061)]’ 
/usr/include/c++/4.4/istream:119: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] 
/usr/include/c++/4.4/istream:123: note:     std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] 
/usr/include/c++/4.4/istream:130: note:     std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>] 
/usr/include/c++/4.4/istream:166: note:     std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>] 
/usr/include/c++/4.4/istream:170: note:     std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>] 
/usr/include/c++/4.4/istream:173: note:     std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>] 
/usr/include/c++/4.4/istream:177: note:     std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>] 
/usr/include/c++/4.4/istream:180: note:     std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char> 

部分:我得到的錯誤

struct iString{ 
    char * chars; 
    unsigned int length; 
    unsigned int capacity; 

    iString(); 
    iString(const char *); 
    iString(const iString&); 
    ~iString(); 

    iString &operator=(const iString&); 
    }; 

// Add other declarations here: 
iString operator+(const iString &a, const iString &b); 
iString operator*(const iString &a, unsigned int scalar); 
iString operator*(unsigned int scalar, const iString &a); 
ostream &operator<<(ostream &out, const iString &b); 
istream &operator>>(istream &in, iString &b); 

部分我使用我的客戶端模塊(主)中的結構的方式:

switch(c) { 
     case 'r': 
     cin >> which; // Reads a, b, c, or d 
     a[which-'a'] = new iString; 
     cin >> *(a[which-'a']); 
     break; 
     case 'w': 
     cin >> which; // Reads a, b, c, or d 
     cout << *(a[which-'a']) << endl; 
     break; 
     case 'n': 
     cin >> which >> temp; 
     a[which-'a'] = new iString(temp.c_str()); 
     break; 
     case 'i': 
     cin >> op1 >> op2; 
     a[op1-'a'] = 

注:字段字符指的是矢量堆上分配

客戶端模塊中的變量定義(主):

int main() { 
    bool done = false; 
    string temp; 
    iString *a[4]; 
    while(!done) { 
    char c; 
    char which; 
    char op1; 
    char op2; 
    int int_op; 
    cerr << "Command?" << endl; // Valid commands: r [a-d] string 
           //     wa, wb, wc, wd, q 
           //     s[a-d][a-d][a-d] 
           //     t [a-d] [a-d] string 
           //     m [a-d] int [a-d] 
           //     p [a-d] [a-d] int 
           //     n [a-d] string 
           //     i [a-d] [a-d] 
           //     = [a-d] [a-d] 
           //     f [a-d] 
           //     e [a-d] 
           //     l [a-d] 
    cin >> c; // Reads r, p, m, s, w, n, i, e, f, q 
    if (cin.eof()) break; 

我用這個定義爲賦值運算符:

iString& iString::operator=(const iString &b) { 
     chars=new char[strlen(b.chars)+1]; 
     strcpy(chars, b.chars); 
     length=capacity=b.length; 
     return *this; 
} 

更改爲刪除[] ...;用於刪除陣列,並把它包含的類定義文件中的「使用命名空間std」我得到了新的錯誤正確的語法:

istring.h:21: error: expected constructor, destructor, or type conversion before ‘&’ token 
istring.h:22: error: expected constructor, destructor, or type conversion before ‘&’ token 
istring.cc: In function ‘std::istream& operator>>(std::istream&, iString&)’: 
istring.cc:59: error: no matching function for call to ‘iString::iString(std::string&)’ 
istring.cc:19: note: candidates are: iString::iString(const iString&) 
istring.cc:11: note:     iString::iString(const char*) 
istring.cc:7: note:     iString::iString() 
istring.h:21: error: expected constructor, destructor, or type conversion before ‘&’ token 
istring.h:22: error: expected constructor, destructor, or type conversion before ‘&’ token 
+0

添加你的變量聲明,我猜''a'是'iString ** a'或類似的東西。 –

+0

在這裏你去先生! –

+0

'刪除a.chars; a = b;'如果有必要,應在'iString' *的賦值運算符中完成'delete'。 – dyp

回答

0

您: 在文件從istring.cc:3包括應該試試;

cout << a[which-'a'] << endl; 
cin >> a[which-'a']; 
+0

我明白你的意思,但不應該將一個iString對象的引用傳遞給<<而不是一個指針?我知道引用是指針,但它們仍然是一種特殊類型的指針:意思是鍵入* cont p; –

0

這是你的問題:您嘗試調用構造函數的參數name是一個std::string。您聲明瞭三個構造函數,但是沒有一個使用std::string

string name; 
    in>>name; 
    iString b(name); 

如果您嘗試了以下操作(如您在代碼中使用的其他操作),該怎麼辦?

iString b(name.c_str()); // use iString(const char *); 

另外,如果你明確指出你想要一個整數來索引你的指針數組呢?

cin >> which; // Reads a, b, c, or d 
    int index = which - 'a';   // explicitly create an int 
    assert(0 <= index && index < 4); // should probably do some checking of input 
    a[index] = new iString; 
    cin >> *(a[index]); 
    break; 

它會使你的代碼更清晰,並避免在你的編譯器錯誤中顯示偏移量。