当前位置:编程学习 > C/C++ >>

c++程序错误是怎么回事

#include<iostream>
#include<string>
using namespace std;
class fruit
{
public:
string name;
void print()
{
 cout<<name<<endl;
}
};
int main()
{
 fruit apple={"apple"};
    apple.print();
 return 0;
}

哪里错了,谢谢

问题分析:

#include<iostream>
#include<string>
using namespace std;
class fruit
{
public:
string name;
void print()
{
 cout<<name<<endl;
}
};
int main()
{
 fruit apple;
 apple.name="apple";
    apple.print();
 return 0;
}

 

你的代码中并没有带参的构造函数


或者如下

#include<iostream>
#include<string>
using namespace std;
class fruit
{
public:
string name;
fruit(string name)
{
 this->name=name;
}
void print()
{
 cout<<name<<endl;
}
};
int main()
{
 fruit apple("apple");
    apple.print();
 return 0;
}

 

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,