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