菜鸟提问大家看看这程序的问题
#include "iostream"
using namespace std;
class Tree
{
public:
void age()
{
cout<<"这棵树的年龄为"<<ages<<"\n";
}
void grow(int n)
{
grows=n+ages;
cout<<"这棵树的年龄为"<<grows<<"\n";
}
private:
int ages,grows;
};
Tree::Tree(int Age)
{
ages=Age;
}
int main()
{
Tree t(12);
t.age();
t.grow(4);
return 0;
}
定义树的类,显示年龄,不知道问题出在哪里
[解决办法]
#include "iostream"using namespace std;class Tree{ public: Tree(int Age);////////////add 这里没加声明 void age() { cout<<"这棵树的年龄为"<<ages<<"\n"; } void grow(int n) { grows=n+ages; cout<<"这棵树的年龄为"<<grows<<"\n"; } private: int ages,grows;};Tree::Tree(int Age){ ages=Age;}int main(){ Tree t(12); t.age(); t.grow(4); return 0;}
[解决办法]
在类声明里没有给出构造函数的声明,如下添加:
public: Tree(int Age);