gpt4 book ai didi

c++ - 错误 : aggregate 'first one' has incomplete type and cannot be defined

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:33 24 4
gpt4 key购买 nike

我写了这个头文件(header1.h):

#ifndef HEADER1_H
#define HEADER1_H

class first ;

//int summ(int a , int b) ;



#endif

和这个源文件(header1.cpp 和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 中运行这个程序时,我给出了这个错误:

aggregate 'first one' has incomplete type and cannot be defined .

最佳答案

您不能将类声明放在.cpp 文件中。您必须将它放在 .h 文件中,否则编译器看不到它。编译 main.cpp 时,类型“first”是 class first;。这根本没有用,因为这不会告诉编译器任何信息(比如第一个大小是多少,或者什么操作在这种类型上有效)。移动这个 block :

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

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

关于c++ - 错误 : aggregate 'first one' has incomplete type and cannot be defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17799906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com