我有一个我试图使用的基本文件。
#ifndef POINT_GUARD
#define POINT_GUARD
//------------------------------------------------------------------------------
struct Point {
int x, y;
Point(int xx, int yy) : x(xx), y(yy) { }
Point() :x(0), y(0) { }
};
//------------------------------------------------------------------------------
inline bool operator==(Point a, Point b) { return a.x==b.x && a.y==b.y; }
//------------------------------------------------------------------------------
inline bool operator!=(Point a, Point b) { return !(a==b); }
//------------------------------------------------------------------------------
#endif // POINT_GUARD
请注意,它被包裹在警卫中。现在将其导入两个不同的文件中。不过我收到了一个错误。
它一旦命中struct Point
就会抱怨它是“重新定义点”。有什么想法可以在这里发生吗?
答案 0 :(得分:1)
我无法使用给定的输入重现错误。我将您的代码放在test.h
中,并为test.cpp
:
#include "test.h"
#include "test.h"
运行g++ -Wall -c test.cpp
不会产生任何错误或警告,并且通过预处理器运行它会显示struct Point
仅被声明一次,因此警卫正在工作。
我猜在你引用的代码之外的其他地方有一个同名的声明?