我写了这个头文件(header1.h):
#ifndef HEADER1_H
#define HEADER1_H
class first ;
//int summ(int a , int b) ;
#endif
Run Code Online (Sandbox Code Playgroud)
和这个源文件(header1.cpp and main.cpp):
#include <iostream>
#include "header1.h"
using namespace std;
class first
{
public:
int a,b,c;
int sum(int a , int b);
};
int first::sum(int a , int b)
{
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#include "header1.h"
using namespace std;
first one;
int main()
{
int j=one.sum(2,4);
cout << j<< endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行这个程序时codeblocks,我给出了这个错误:
聚合'第一个'具有不完整的类型,无法定义.
您不能将类声明放在.cpp文件中.你必须把它放在.h文件中,否则它对编译器是不可见的.编译main.cpp时,类型"first"是class first;.这根本没用,因为这对编译器没有任何意义(比如首先是什么大小或者什么操作在这种类型上有效).移动这个块:
class first
{
public:
int a,b,c;
int sum(int a , int b);
};
Run Code Online (Sandbox Code Playgroud)
从header1.cpp到header1.h并class first;在header1.h中删除
| 归档时间: |
|
| 查看次数: |
37651 次 |
| 最近记录: |