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;
}
这些是我为结构编写的运算符:
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);
我得到的部分错误:
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>
我在客户端模块(main)中使用结构的部分方式:
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;
}
更改为删除[] ...;删除数组的正确语法,并在包含类定义的文件中放入“using namespace std”,我得到了新的错误: 在istring.cc:3中包含的文件中:
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 :(得分:0)
你应该试试;
cout << a[which-'a'] << endl;
cin >> a[which-'a'];
答案 1 :(得分:0)
这是您的问题:您正在尝试使用name
的{{1}}参数调用构造函数。您声明了三个构造函数,但没有一个构造函数使用std::string
。
std::string
如果您尝试以下内容,就像您在代码中的其他地方使用过一样,该怎么办?
string name;
in>>name;
iString b(name);
另外,如果你明确表示你想要一个整数来索引你的指针数组呢?
iString b(name.c_str()); // use iString(const char *);
它会使您的代码更清晰,并避免在编译器错误中显示偏移量。