g++编译错误基本对象构造

g++ compile error basic object construction

本文关键字:对象 编译 错误 g++      更新时间:2023-10-16

我收到错误:

thing.cpp:5:1: error: ‘SquareThing’ does not name a type
 SquareThing::SquareThing()
 ^
compilation terminated due to -Wfatal-errors.
make: *** [thing.o] Error 1

我的thing.h文件:

#define THING_H_
#ifndef THING_H_
#include <vector>
#include <iostream>
class SquareThing{
public:
        SquareThing();
        //other functions
private:
    int something;
    //more members
};
#endif

我的任何东西.cpp:

#include "thing.h"
#include <vector>
#include <iostream>
using namespace std;
SquareThing::SquareThing()
{
     something = 3;
}
//more functions below

这看起来太初级了,但我似乎真的找不到错误。非常感谢您的帮助。

这两个语句是向后的:

#define THING_H_
#ifndef THING_H_

你的意思当然是:

#ifndef THING_H_
#define THING_H_

在向后的顺序中,您确保标头的正文永远不会进入,这并不是很有用。

您需要切换宏的顺序:

#define THING_H_ // define the macro
#ifndef THING_H_ // if the macro is not defined (well, it is always defined...)

应该是

#ifndef THING_H_ // if the macro hasn't been defined ...
#define THING_H_ // ... define it