【发布时间】:2018-04-16 04:45:14
【问题描述】:
我一直在搜索其他论坛和问题,但似乎找不到与我的问题相关的答案。我不断收到这个错误,在 .cpp 文件中显示“重新定义 'Shape'”,它出现在构造函数和函数中。
形状.h
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
using namespace std;
class Shape {
private:
string name;
public:
Shape();
Shape(string name);
string getName() const;
friend ostream& operator << (ostream& output, const Shape & shape);
};
#endif // SHAPE_H
形状.cpp
#include <iostream>
#include "Shape.h"
using namespace std;
Shape::Shape() {
this->name = "Shape";
}
Shape::Shape(string name) {
this->name = name;
}
string Shape::getName() const {
return name;
}
ostream& operator << (ostream& output, const Shape & shape) {
output << shape.getName();
return output;
}
【问题讨论】:
-
无法重现,因此我们需要更多信息。将
using namespace std;放入标题时要非常小心。对于没有预料到的人来说,这可能是一个非常令人讨厌的惊喜。 -
听起来这段代码工作正常。
-
我的教授给了我这个代码来开始一个项目,但我没有接触过它,所以我很困惑为什么它会给我这些错误。如果我还有更多工作要做,我会把它给你。
-
你有
main()吗?你包括"shape.h"还是"shape.cpp"?