#include<iostream>using namespace std;class Student{public:Student(int,int);void display();private:int number;int score;};Student::Student(int a,int b){number=a;score=b;}void Student::display(){cout<<number<<":"<<score<<endl;} int main(){Student* p;p= new Student[5]; //為什么總是提示沒有合適的構(gòu)造函數(shù),這里我是想動態(tài)建立一個包含5個對象對象數(shù)組,并用指針指向首對象,并不是想調(diào)用構(gòu)造函數(shù)Student student[5]={Student(1,89),Student(2,92),Student(3,98),Student(4,95),Student(5,81)}; int i;for(i=0;i<5;i=i+2){p[i].display();}cout<<endl;delete p;return 0;}
3 回答

長風(fēng)秋雁
TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個贊
構(gòu)造函數(shù)不對,是兩個參數(shù)(int,int)。
所有你得:
p=new Student(1,1)[5];
否則自己加一個構(gòu)造函數(shù)
Student::Student();
然后再加一個輸入的函數(shù)input(...);
才能p=new Student()[5];
*(p+1).input(...);

元芳怎么了
TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個贊
Student* = new Student[5]; |
要那樣開辟內(nèi)存,你必須要有默認(rèn)參數(shù)的構(gòu)造函數(shù),就是說
class Student { public : Student(); // 默認(rèn)參數(shù)的構(gòu)建(比如,默認(rèn)name,0分) Student( int , int ); void display(); private : int number; int score; }; |

桃花長相依
TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個贊
Student()
{}
自己再加一個空構(gòu)造函數(shù),當(dāng)你自己定義構(gòu)造函數(shù)時,系統(tǒng)不再生成默認(rèn)空構(gòu)造函數(shù)!
- 3 回答
- 0 關(guān)注
- 430 瀏覽
添加回答
舉報
0/150
提交
取消