-3
我寫這段代碼只是爲了練習來測試複製構造函數和析構函數的概念,但它顯示了一個錯誤,即共享指針不是一個模板,我最指針是未定義按編譯器。爲什麼我得到錯誤「(指針的名稱)不是模板)?
我想知道什麼是錯。
class MyString
{
public:
MyString(); // default ctor
MyString(string const &); // ctor with ordinary string argument
MyString(MyString const &); // copy ctor
MyString(MyString &&); // move ctor – discussed later
~MyString(); // destructor
MyString& operator=(MyString const &rhsObject); // assignment operator
void showString(); // outputs the String in the object
private:
shared_ptr<string> rep;
}; // end class MyString
MyString::MyString()
{
// creates an object with empty string
rep = make_shared<string>();
}
MyString::MyString(string str)
{
// creates an object with an ordinary string pointed to
rep = make_shared<string>(str);
}
MyString::MyString(MyString const &ob)
{
// creates an object with String as in ob
rep = make_shared<string>(ob.rep);
}
MyString::~MyString()
{
}
MyString & MyString::operator=(const MyString &ob)
{
// resets the value of calling object to the value of ob
rep = make_shared<string>(ob.rep);
return *this; // 「this」 pointer explained below
}
void MyString::showString()
{
// outputs the string pointed to by rep
cout << *rep;
}
void main()
{
MyString a("Hello"), b = "CS 570", c("Students");
// constructor MyString(char *) creates objects a, b, and c
MyString d; // default constructor MyString() creates d
MyString e(a), f = c;
// copy constructor MyString(MyString &) creates objects e and f
cout << "String in object a = ";
a.showString();
cout << endl; cout << "String in object b = "; b.showString(); cout << endl;
cout << "String in object c = ";
c.showString(); cout << endl;
cout << "String in object d = ";
d.showString(); cout << endl;
cout << "String in object e = ";
e.showString(); cout << endl;
cout << "String in object f = ";
f.showString(); cout << endl;
d = c;
cout << "Object d reset to have String of c : "; d.showString(); cout << endl;
c = a;
cout << "Object c reset to have String of a : "; c.showString();
cout << endl;
//Copy constructor and this pointer 7
cout << "Object d’s string remains as before : "; d.showString();
cout << endl;
} // end main function
縮進你的代碼。 – gsamaras
你有沒有'#include'? –
user463035818
請包括完整的錯誤信息,行號有很多幫助。 –