读书人

初学者提问大家看看这程序的有关问题

发布时间: 2012-04-14 17:14:21 作者: rapoo

菜鸟提问大家看看这程序的问题
#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;
}
定义树的类,显示年龄,不知道问题出在哪里

[解决办法]

C/C++ code
#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;}
[解决办法]
在类声明里没有给出构造函数的声明,如下添加:
C/C++ code
  public:      Tree(int Age); 

读书人网 >C++

热点推荐