[英]Error : aggregate 'first one' has incomplete type and cannot be defined
我写了这个头文件(header1.h)
:
#ifndef HEADER1_H
#define HEADER1_H
class first ;
//int summ(int a , int b) ;
#endif
和这个源文件(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;
}
#include <iostream>
#include "header1.h"
using namespace std;
first one;
int main()
{
int j=one.sum(2,4);
cout << j<< endl;
return 0;
}
但是当我在codeblocks
运行这个程序时,我给出了这个错误:
聚合'第一个'具有不完整的类型,无法定义。
您不能将类声明放在.cpp文件中。 你必须把它放在.h文件中,否则它对编译器是不可见的。 编译main.cpp时,类型“first”是class first;
。 这根本没用,因为这对编译器没有任何意义(比如首先是什么大小或者这种类型的操作有效)。 移动这个块:
class first
{
public:
int a,b,c;
int sum(int a , int b);
};
从header1.cpp到header1.h并class first;
删除class first;
在header1.h中
如果您也使用主函数,只需在顶部定义类并稍后定义主函数。 没有必要显式创建单独的头文件。
您需要在头文件中声明整个类(包含在实际使用该类的每个位置)。 OTERhwise,编译器不会知道如何在类中“找到” sum
(或者它应该为该类保留多少空间)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.