【问题标题】:g++ compile error basic object constructiong++编译报错基本对象构造
【发布时间】:2013-12-08 02:17:47
【问题描述】:

我收到错误消息:

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

我的东西.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

这似乎太简陋了,但我似乎真的找不到错误。非常感谢任何帮助。

【问题讨论】:

  • 通常,标头保护MAZE_H_ 会反映文件的名称,以避免重复。如果您的任何其他文件也定义了MAZE_H_,则您的整个thing.h 标头将被跳过...
  • 抱歉,我在放置“MAZE_H_”而不是“THING_H_”时犯了一个根本性错误。我的实际代码有 'THING_H_' 但仍然无法编译。

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


【解决方案1】:

这两条语句是倒退的:

#define THING_H_
#ifndef THING_H_

你肯定是说:

#ifndef THING_H_
#define THING_H_

在倒序中,您确保标题的主体从不启动,这并不是非常有用。

【讨论】:

【解决方案2】:

您需要切换宏的顺序:

#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多