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

时间:2013-12-08 02:17:47

标签: c++ constructor compiler-errors header-files

我收到错误:

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

my thing.h文件:

#define THING_H_
#ifndef THING_H_
#include <vector>
#include <iostream>
class SquareThing{

public:
        SquareThing();
        //other functions
private:
    int something;
    //more members

};
#endif

Any my thing.cpp:

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

这似乎太简陋但我似乎无法找到错误。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

这两个陈述是落后的:

#define THING_H_
#ifndef THING_H_

你当然意味着:

#ifndef THING_H_
#define THING_H_

在向后的顺序中,您确保标题的主体从未踢入,这不是非常有用。

答案 1 :(得分:0)

您需要切换宏的顺序:

#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