C++ 没有合适的默认构造函数可用
#include <iostream>
class Student
{
private:
    int id;
    char *name;
    int age;
public:
    Student(int the_id,char *the_name,int the_age)
    {
        id = the_id;
        name = the_name;
        age = the_age;
    }
    int getage()
    {
        return age;
    }
    char * getname()
    {
        return name;
    }
    int getid()
    {
        return id;
    }
};
class Item :public Student
{
public:
    Item()
    { //此处报错“没有合适的默认构造函数可用”

    }
    float ave()
    {

    }
private:
    float Chinese,math,English;
};
int main()
{
    char name[20] = "Xiao Ming";
    Student xiaoming = Student(101011,name,18);
    std::cout<<xiaoming.getname()<<":"<<xiaoming.getid()<<" "<<xiaoming.getage();
}

解决方法(一):
Student类中添加一个不带参数的默认构造函数!

...
class Student
{
public:
    Student(void)
    {
    }
    Student(int the_id, char* the_name, int the_age)
    {
        id = the_id;
        name = the_name;
        age = the_age;
    }
    ...

解决方法(二):

...
class Student
{
public:
    Student(int the_id=0, char* the_name=0, int the_age=0)
    {
        id = the_id;
        name = the_name;
        age = the_age;
    }
...
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇