【问题标题】:Error : aggregate 'first one' has incomplete type and cannot be defined错误:聚合“第一个”类型不完整,无法定义
【发布时间】:2013-07-21 21:58:48
【问题描述】:

这个头文件我已经写好了(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 运行这个程序时,我给出了这个错误:

聚合“第一个”类型不完整,无法定义。

【问题讨论】:

  • 对于其他人来说,这个错误可能还有其他原因;包括必需的标题。

标签: c++ codeblocks


【解决方案1】:

您不能将类声明放在 .cpp 文件中。您必须将它放在 .h 文件中,否则编译器将看不到它。当 main.cpp 被编译时,类型“first”是class first;。这根本没有用,因为这不会告诉编译器任何东西(比如首先是什么大小或在这种类型上哪些操作是有效的)。移动这个块:

class first
{
public:
    int a,b,c;
    int sum(int a , int b);
};

从 header1.cpp 到 header1.h 并去掉 header1.h 中的class first;

【讨论】:

    【解决方案2】:

    如果您也使用主函数,只需在顶部定义类,然后再定义主函数。不需要显式创建单独的头文件。

    【讨论】:

      【解决方案3】:

      您需要在头文件中声明整个类(该文件包含在实际使用该类的每个位置)。否则,编译器将不知道如何在类中“找到”sum(或者它应该为类保留多少空间)。

      【讨论】:

        猜你喜欢
        • 2021-06-24
        • 2020-11-25
        • 1970-01-01
        • 2014-05-20
        • 2020-02-21
        • 1970-01-01
        • 2012-07-29
        • 1970-01-01
        相关资源
        最近更新 更多