Lera
Lera

Reputation: 33

C++ code generating errors LNK2005 & LNK1169 on VS2019

The following code generates errors LNK2005 & LNK1169 on Visual Studio 2019.

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.

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.

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.
Interestingly enough, if I remove any reference to OverloadingPlus.h file, and add #include Complex.h to main.cpp, the problem goes away.
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.
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.
I attempted to declare a diferent operator+ overload inside the class (you can see it commented out), but it produces the same errors.
Commented all references to any other files trying to make sure Complex definitions are only included once. 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

#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

#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

#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;
    }
}

Upvotes: 1

Views: 632

Answers (2)

Lera
Lera

Reputation: 33

The issue seems to be related to 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.

Upvotes: 1

Hajo Kirchhoff
Hajo Kirchhoff

Reputation: 2075

You forgot to "inline" your 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.

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).

Every time you include "OverloadingPlus.h" in one of your .cpp files, you add another definition for void OverloadingPlus to your program. The linker detects this and cannot decide on "the right one" and so gives an error.

Upvotes: 0

Related Questions