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;
}