首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-04-14 
菜鸟提问大家看看这程序的问题#include iostreamusing namespace stdclass Tree{public:void age(){cou

菜鸟提问大家看看这程序的问题
#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); 

热点排行