[英]C++ code generating errors LNK2005 & LNK1169 on VS2019
以下代码在 Visual Studio 2019 上生成错误 LNK2005 和 LNK1169。
LNK2005: "void__cdecl OverloadingPlus(void)" (?OverloadingPlus@@YAXXZ) already defined in main.obj
LNK1169: one or more multiply defined symbols found
我通过学习 Udemy 的课程达到了这一点,讲师似乎对此没有任何问题。
类似的问题似乎与使用全局变量有关,在 header 文件中定义函数,而不是使用#ifndef
守卫......但据我所知,情况似乎并非如此。
此问题仅在包含operator+
重载后才开始出现,但operator<<
不会触发任何错误,即使它们已以相同方式声明和定义。
有趣的是,如果我删除对OverloadingPlus.h
文件的任何引用,并将#include Complex.h
添加到 main.cpp,问题就会消失。
我看不到在OverloadingPlus.h
中包含Complex.h
如何专门为operator+
的多重定义做出贡献,即使它在整个项目中只包含一次,并且定义在Complex.cpp
中实现,而不是在 header 文件中实现,正如在类似问题的答案中指出的那样。
还尝试使用#ifndef
语句围绕OverloadingPlus.h
的内容,以确保它只使用一次而不会改变任何事情。
我试图在 class 中声明一个不同的operator+
重载(您可以看到它已被注释掉),但它会产生相同的错误。
注释了对任何其他文件的所有引用,以确保仅包含一次Complex
定义。 也没有帮助。
代码如下:
主文件
//#include "OverloadingAssignmentOperator.h"
//#include "OverloadingLeftBitShiftOperator.h"
//#include "ComplexNumberClass.h"
#include "OverloadingPlus.h"
int main() {
//OverloadingAssignmentOperator();
//OverloadingLeftBitShiftOperator();
//ComplexNumberClass();
OverloadingPlus();
return 0;
}
重载Plus.h
#ifndef OVERLOADING_PLUS_H
#define OVERLOADING_PLUS_H
#include "Complex.hpp"
using namespace complex;
void OverloadingPlus() {
Complex c1(1, 4);
Complex c2(3, 2);
std::cout << c1 + c2 << std::endl;
}
#endif
复杂的.hpp
#ifndef COMPLEX_HPP
#define COMPLEX_HPP
#include <iostream>
namespace complex {
class Complex {
private:
double real;
double imaginary;
public:
Complex();
Complex(double, double);
Complex(const Complex&);
const Complex& operator=(const Complex& other);
//const Complex operator+(const Complex& r);
double getReal() const { return real; }
double getImaginary() const { return imaginary; }
};
Complex operator+(const Complex& l, const Complex& r);
std::ostream& operator<<(std::ostream& out, const Complex& c);
}
#endif // !__COMPLEX_HPP__
复杂的.cpp
#include "Complex.hpp"
namespace complex {
Complex::Complex() : real(0), imaginary(0) {}
Complex::Complex(double r, double i) : real(r), imaginary(i) {}
Complex::Complex(const Complex& other) {
std::cout << "Copy constructor..." << std::endl;
real = other.real;
imaginary = other.imaginary;
}
const Complex& Complex::operator=(const Complex& other) {
real = other.real;
imaginary = other.imaginary;
return *this;
}
//const Complex Complex::operator+(const Complex& r) {
// return Complex(real + r.getReal(), imaginary + r.getImaginary());
//}
//
Complex operator+(const Complex& l, const Complex& r) {
return Complex(l.getReal()+r.getReal(), l.getImaginary()+r.getImaginary());
}
std::ostream& operator<<(std::ostream& out, const Complex& c) {
out << "(" << c.getReal() << "," << c.getImaginary() << ")";
return out;
}
}
该问题似乎与VS2019有关。 将文件重命名为不同的名称、重建项目并将文件重命名为其原始名称解决了该问题。
虽然像其他人建议的那样使用inline
确实绕过了这个问题,但它并没有解决这个特定问题,因为冲突文件没有被多次包含。
您忘记“内联”您的OverloadingPlus
。
inline void OverloadingPlus() {
Complex c1(1, 4);
Complex c2(3, 2);
std::cout << c1 + c2 << std::endl;
}
应该使 linker 错误 go 消失。
可能发生的情况是:您在多个编译单元中包含“OverloadingPlus.h”(尽管您未能在问题中提供#include 的所有实例)。
每次在您的一个.cpp 文件中包含“OverloadingPlus.h”时,都会在程序中添加另一个void OverloadingPlus
定义。 linker 检测到这一点,无法决定“正确的”,因此会出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.