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
添加你的變量聲明,我猜''a'是'iString ** a'或類似的東西。 –
在這裏你去先生! –
'刪除a.chars; a = b;'如果有必要,應在'iString' *的賦值運算符中完成'delete'。 – dyp