读书人

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

发布时间: 2012-03-30 17:32:09 作者: rapoo

多文件形式编写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

读书人网 >C++

热点推荐