简体   繁体   English

VS2019 上的 C++ 代码生成错误 LNK2005 和 LNK1169

[英]C++ code generating errors LNK2005 & LNK1169 on VS2019

The following code generates errors LNK2005 & LNK1169 on Visual Studio 2019.以下代码在 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

I reached this point by following a course on Udemy, where the instructor seems to have no problems with it.我通过学习 Udemy 的课程达到了这一点,讲师似乎对此没有任何问题。

Similar questions seem to be related to using global variables, defining functions in the header file, not using #ifndef guards... but as far as I can tell, that doesn't seem to be the case.类似的问题似乎与使用全局变量有关,在 header 文件中定义函数,而不是使用#ifndef守卫......但据我所知,情况似乎并非如此。

This problem started appearing only after the operator+ overload was included, but the operator<< doesn't trigger any errors, even though they have been declared and defined in the same manner.此问题仅在包含operator+重载后才开始出现,但operator<<不会触发任何错误,即使它们已以相同方式声明和定义。
Interestingly enough, if I remove any reference to OverloadingPlus.h file, and add #include Complex.h to main.cpp, the problem goes away.有趣的是,如果我删除对OverloadingPlus.h文件的任何引用,并将#include Complex.h添加到 main.cpp,问题就会消失。
I fail to see how including Complex.h in OverloadingPlus.h is contributing to a multiple definition of the operator+ exclusively, even though it is only included once in the entire project and the definitions are implemented in Complex.cpp and not in the header file, as pointed out in answers to similar problems.我看不到在OverloadingPlus.h中包含Complex.h如何专门为operator+的多重定义做出贡献,即使它在整个项目中只包含一次,并且定义在Complex.cpp中实现,而不是在 header 文件中实现,正如在类似问题的答案中指出的那样。
Also tried surrounding the content of OverloadingPlus.h with an #ifndef statement just to make sure it is only used once which did not change a thing.还尝试使用#ifndef语句围绕OverloadingPlus.h的内容,以确保它只使用一次而不会改变任何事情。
I attempted to declare a diferent operator+ overload inside the class (you can see it commented out), but it produces the same errors.我试图在 class 中声明一个不同的operator+重载(您可以看到它已被注释掉),但它会产生相同的错误。
Commented all references to any other files trying to make sure Complex definitions are only included once.注释了对任何其他文件的所有引用,以确保仅包含一次Complex定义。 Didn't help either.也没有帮助。

The code is the following:代码如下:
main.cpp主文件

//#include "OverloadingAssignmentOperator.h"
//#include "OverloadingLeftBitShiftOperator.h"
//#include "ComplexNumberClass.h"
#include "OverloadingPlus.h"

int main() {
    //OverloadingAssignmentOperator();
    //OverloadingLeftBitShiftOperator();
    //ComplexNumberClass();
    OverloadingPlus();

    return 0;
}

OverloadingPlus.h重载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 

Complex.hpp复杂的.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__

Complex.cpp复杂的.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;
    }
}

The issue seems to be related to VS2019.该问题似乎与VS2019有关。 Renaming the file to something different, rebuilding the project and renaming the file back to its original name fixed the issue.将文件重命名为不同的名称、重建项目并将文件重命名为其原始名称解决了该问题。

While using inline as others suggested did circumvent the issue, it does not solve this specific problem as the conflicting file was not being included multiple times.虽然像其他人建议的那样使用inline确实绕过了这个问题,但它并没有解决这个特定问题,因为冲突文件没有被多次包含。

You forgot to "inline" your OverloadingPlus .您忘记“内联”您的OverloadingPlus

inline void OverloadingPlus() {
    Complex c1(1, 4);
    Complex c2(3, 2);

    std::cout << c1 + c2 << std::endl;
}

should make the linker error go away.应该使 linker 错误 go 消失。

What probably happened is: You have included "OverloadingPlus.h" in more than one compilation unit (although you failed to provide all instances of your #include in your question).可能发生的情况是:您在多个编译单元中包含“OverloadingPlus.h”(尽管您未能在问题中提供#include 的所有实例)。

Every time you include "OverloadingPlus.h" in one of your.cpp files, you add another definition for void OverloadingPlus to your program.每次在您的一个.cpp 文件中包含“OverloadingPlus.h”时,都会在程序中添加另一个void OverloadingPlus定义。 The linker detects this and cannot decide on "the right one" and so gives an error. linker 检测到这一点,无法决定“正确的”,因此会出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM