#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]; //為什么總是提示沒有合適的構造函數,這里我是想動態建立一個包含5個對象對象數組,并用指針指向首對象,并不是想調用構造函數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 回答

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

元芳怎么了
TA貢獻1798條經驗 獲得超7個贊
Student* = new Student[5]; |
要那樣開辟內存,你必須要有默認參數的構造函數,就是說
class Student { public : Student(); // 默認參數的構建(比如,默認name,0分) Student( int , int ); void display(); private : int number; int score; }; |
- 3 回答
- 0 關注
- 442 瀏覽
添加回答
舉報
0/150
提交
取消