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

多文件形式编写c++的有关问题

2012-03-30 
多文件形式编写c++的问题first.h#ifndef FIRST_H#define FIRST_H#include iostreamusing namespace std

多文件形式编写c++的问题
first.h


#ifndef FIRST_H
#define FIRST_H
#include <iostream>
using namespace std;

class Apple
{
  public:
  Apple(int a,int b);
  double cnr() const;
  int fn(int x)const;
  private:
  int n;
  int r;
};

#endif
*****************************************************************************************


function.cpp


#include <iostream>
#include "first.h"
using namespace std;


Apple::Apple(int a,int b)
{
  n = a, r = b;
}

double Apple::cnr() const
{
  double sum;
  sum = fn(n) / (fn(r) * fn(n-r));

  return sum;

}

int Apple::fn(int x) const
{
  if(n <= 1)
  {
  return 1;
  }
  else
  {
  return n * f(n - 1);
  }
}


*******************************************************************
main.cpp


#ifndef FIRST_H
#define FIRST_H
#include <iostream>
#include "first.h"

using namespace std;

int main()
{
  Apple a(4,3);
  cout << a.cnr() <<endl;
  return 0;
}
#endif



************************************
程序写完之后,编译器提示Apple was not declared in this scope.
这似乎是说Apple类并没有被定义。可是我已经在first.h中定义了啊,而且也将first.h包含进main.cpp中了。
怎么还会出现这种状况?我用的是codeblocks10.05

[解决办法]
main.cpp不要这个
#ifndef FIRST_H
#define FIRST_H

#endif

热点排行