2016-03-14 24 views
-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 
+1

縮進你的代碼。 – gsamaras

+1

你有沒有'#include '? – user463035818

+0

請包括完整的錯誤信息,行號有很多幫助。 –

回答

-1
#include <string> 
#include <memory> 
#include <iostream> 

class MyString 
{ 
    public: 
    MyString(); 
    MyString(std::string str1); 
    MyString(MyString const &); 
    MyString(MyString &&); 
    ~MyString(); 
    MyString& operator=(MyString const &rhsObject); 
    std::string showString(); 

private: 
    static std::shared_ptr<std::string> rep; 
}; 


MyString::MyString() 
{ 
    rep = std::make_shared<std::string>(); 
} 


MyString::MyString(std::string str1) 
{ 
    rep = std::make_shared<std::string>(str1); 
} 


MyString::MyString(MyString const &ob) 
{ 
    rep = std::make_shared<std::string>(ob.rep); 
} 

MyString::~MyString() 
{ 

} 


MyString & MyString::operator=(const MyString &ob) 
{ 
    rep = std::make_shared<std::string>(ob.rep); 
    return *this; 
} 


std::string MyString::showString() 
{ 
    return *rep; 
} 

int main() 
{ 

    MyString a("Hello"), 
      b("CS 570"), 
      c("Students"); 

    MyString d; 
    MyString e(a), f = c; 
    std::cout << a.showString() << std::endl; 
    std::cout << "\n" << a.showString() << std::endl; 
    std::cout << "String in object b = " << b.showString() << std::endl; 
    std::cout << "String in object c = "; 
    std::cout << c.showString() << std::endl; 
    std::cout << "String in object d = "; 
    std::cout << d.showString() << std::endl; 
    std::cout << "String in object e = "; 
    std::cout << e.showString() << std::endl; 
    std::cout << "String in object f = "; 
    std::cout << f.showString() << std::endl; 
    d = c; 
    std::cout << "Object d reset to have String of c : " << d.showString() << std::endl; 
    c = a; 
    std::cout << "Object c reset to have String of a : " << c.showString() << std::endl; 
    std::cout << "Object d’s string remains as before : " << d.showString() << std::endl; 

return 0; 

}

好吧,這是一個更新我以前的例子,我重申寫下你所有的代碼,並把100多個錯誤打倒到1個錯誤,我故意留下這個錯誤,因爲這是你會遇到的一些事情,必須學會獨立解決。

我會建議,儘管這很小,將其分解爲您的主要源代碼,您的類的頭文件和函數源代碼,以便您更輕鬆地調試您的東西。

至於剩下的問題,你爲什麼使用所有這些構造函數?

你可以用1個構造函數(2,甚至3)來做任何事,其餘的可以是函數。

你問了什麼是錯的,答案是你所做的大部分代碼都是重寫和修復最小的問題,這些問題可以讓你進步和學習修復你自己的代碼。

  1. 不要只使用構造函數,做常量和變量引用參數進行更多的研究,閱讀了關於指針我真的建議重新寫了這一切,你已經得到它安裝程序現在將與衝突的方式內存頭。
相關問題