编译时出现重定义错误
Redefinition error at compile time
我一直在搜索其他论坛和问题,但我似乎找不到与我的问题相关的答案。我不断收到此错误,在.cpp文件中显示"重新定义'形状'",并且它同时出现在构造函数和函数中。
形状.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;
}
我不断收到此错误,在.cpp文件中显示"重新定义'形状'">?没有Shape
只定义一次,只需确保两者都是不同的文件。以及 2 行下方的评论 Shape.cpp
//#include <iostream> /* already included in shape.h */
#include "Shape.h"
//using namespace std; /* already there in shape.h */
您可能需要像下面这样打电话
int main() {
Shape obj("Nick Morin");
using std::cout<<obj;
return 0;
}
阅读这篇文章 为什么"使用命名空间 std"被认为是不好的做法?
相关文章:
- 使用命名空间时出现多个定义错误
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 链接阶段出现多重定义错误
- 避免模板类中的重定义错误
- 即使我没有包含多个文件,C++中的多个定义错误
- 跨多个类的全局变量而不会出现重定义错误?
- 尝试运行 wasm 函数时出现模块未定义错误
- C++ 预期的左大括号以及重定义错误
- C++:成员的越界声明必须是纯虚函数的定义错误
- c++中数组的未定义错误
- Visual C:模板类中的自定义错误消息
- 为什么C++模板不会导致多个定义错误?
- 只有一个定义/声明时标头声明变量的多堆定义错误
- C++ 在多个其他类中使用单个类 - 编译时出现多个定义错误
- 基类未定义.错误 C2504
- Q 斯坦达项重定义错误
- 可视化C++中的结构定义错误
- VC++ C2011 重定义错误 - 未使用的头文件