2017-10-08 205 views
-1

嘗試在類內部打印函數來打印聲明的變量。C++類錯誤沒有指定類型

class MovieData{ 
private: 
    int year, runtime; 
    string title, director; 
public: 
    MovieData(string t, string d, int y, int rt){ 
     year = y; 
     runtime = rt; 
     title = t; 
     director = d; 
    } 

    print{ 
     cout << title << " (" << year << "). Directed by " << director << ". (" << rt << " minutes)"; 
    } 
}; 

錯誤就出來這樣的:

main.cpp:20:9: error: ‘print’ does not name a type 
    print{ 
    ^
main.cpp: In function ‘void doIt(std::string, std::string, int, int)’: 
main.cpp:33:5: error: ‘class MovieData’ has no member named ‘print’ 
md.print(); 

回答

5

你需要print後的括號以及返回類型,就像這樣:void print() {...}

+1

謝謝我正在學習cPP的過程中,這是我第一次用C++編寫的類,我應該看到它只是由於某種原因而滑落了我的腦海。謝謝 :) – Coderz