如何初始化dvd1?在C++ 98中,'dvd1'必須由構造函數初始化,而不是'{...}'
C:\Users\SuperUser\Google Drive>g++ -o a Question1.cpp
Question1.cpp: In function 'int main()':
Question1.cpp:39:76: error: in C++98 'dvd1' must be initialized by constructor, not by '{...}'
dvd dvd1 = {"Zombie","BBC","James","English","Alw",2012,20170601,20170630};
#include <iostream>
#include <string>
using namespace std;
class dvd
{
string movieName,movieProdComp,movieLeadActor,movieLanguage,renterName;
int releaseYear,rentDate,returnDate;
public:
dvd(string mn,string mpc,string mla,string ml, string rn,int ry,int rd,int retd);
void print();
};
dvd::dvd(string mn,string mpc,string mla,string ml, string rn, int ry,int rd,int retd){
movieName=mn;
movieProdComp=mpc;
movieLeadActor=mla;
movieLanguage=ml;
renterName=rn;
releaseYear=ry;
rentDate=rd;
returnDate=retd;
}
void dvd::print(){
cout<<"Movie:"<<movieName<<endl;
cout<<"Production:"<<movieProdComp<<endl;
cout<<"Starring:"<<movieLeadActor<<endl;
cout<<"Language:"<<movieLanguage<<endl;
cout<<"Rented By:"<<renterName<<endl;
cout<<"Released:"<<releaseYear<<endl;
cout<<"Rent Date:"<<rentDate<<endl;
cout<<"Return Date:"<<returnDate<<endl;
}
int main(){
dvd();
dvd dvd1 = {"Zombie","BBC","James","English","Alw",2012,20170601,20170630};
dvd1.print();
}
謝謝。我會在9分鐘內接受這個答案。 – null
Nit:僅支持C++ 11中具有顯式構造函數*的類。 C++ 98可以通過'struct simple {int i,j; } value = {0,1};' –
@MartinBonner謝謝,我添加了關於正確構造函數的註釋。關於struct初始化,我的*個人意見*是省略這樣的通知。 C++ *希望結構是類,所以我們不要談論它失敗的時刻* :) – iehrlich