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>
クライアント モジュール (メイン) で構造体を使用する方法の一部:
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'] =
注 : フィールド chars は、割り当てられたベクター ヒープを参照します。
クライアント モジュール (メイン) の変数定義:
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」を入れると、新しいエラーが発生します: In file included from 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