C++ code generating errors LNK2005 & LNK1169 on VS2019 - c++

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

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.

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.

Related

Use non-member functions in Qt [duplicate]

Given this code sample:
complex.h :
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
class Complex
{
public:
Complex(float Real, float Imaginary);
float real() const { return m_Real; };
private:
friend std::ostream& operator<<(std::ostream& o, const Complex& Cplx);
float m_Real;
float m_Imaginary;
};
std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
#endif // COMPLEX_H
complex.cpp :
#include "complex.h"
Complex::Complex(float Real, float Imaginary) {
m_Real = Real;
m_Imaginary = Imaginary;
}
main.cpp :
#include "complex.h"
#include <iostream>
int main()
{
Complex Foo(3.4, 4.5);
std::cout << Foo << "\n";
return 0;
}
When compiling this code, I get the following error:
multiple definition of operator<<(std::ostream&, Complex const&)
I've found that making this function inline solves the problem, but I don't understand why. Why does the compiler complain about multiple definition? My header file is guarded (with #define COMPLEX_H).
And, if complaining about the operator<< function, why not complain about the public real() function, which is defined in the header as well?
And is there another solution besides using the inline keyword?
The problem is that the following piece of code is a definition, not a declaration:
std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
You can either mark the function above and make it "inline" so that multiple translation units may define it:
inline std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
Or you can simply move the original definition of the function to the "complex.cpp" source file.
The compiler does not complain about "real()" because it is implicitly inlined (any member function whose body is given in the class declaration is interpreted as if it had been declared "inline"). The preprocessor guards prevent your header from being included more than once from a single translation unit ("*.cpp" source file"). However, both translation units see the same header file. Basically, the compiler compiles "main.cpp" to "main.o" (including any definitions given in the headers included by "main.cpp"), and the compiler separately compiles "complex.cpp" to "complex.o" (including any definitions given in the headers included by "complex.cpp"). Then the linker merges "main.o" and "complex.o" into a single binary file; it is at this point that the linker finds two definitions for a function of the same name. It is also at this point that the linker attempts to resolve external references (e.g. "main.o" refers to "Complex::Complex" but does not have a definition for that function... the linker locates the definition from "complex.o", and resolves that reference).
Move implementation to complex.cpp
Right now after including this file implementation is being compiled to every file.
Later during linking there's a obvious conflict because of duplicate implementations.
::real() is not reported because it's inline implicitly (implementation inside class definition)
I was having this problem, even after my source and header file were correct.
It turned out Eclipse was using stale artifacts from a previous (failed) build.
To fix, use Project > Clean then rebuild.
An alternative to designating a function definition in a header file as inline is to define it as static. This will also avoid the multiple definition error.

Operator overloading on the global scope in qt/c++ is giving me the multiple definition compile error [duplicate]

Given this code sample:
complex.h :
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
class Complex
{
public:
Complex(float Real, float Imaginary);
float real() const { return m_Real; };
private:
friend std::ostream& operator<<(std::ostream& o, const Complex& Cplx);
float m_Real;
float m_Imaginary;
};
std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
#endif // COMPLEX_H
complex.cpp :
#include "complex.h"
Complex::Complex(float Real, float Imaginary) {
m_Real = Real;
m_Imaginary = Imaginary;
}
main.cpp :
#include "complex.h"
#include <iostream>
int main()
{
Complex Foo(3.4, 4.5);
std::cout << Foo << "\n";
return 0;
}
When compiling this code, I get the following error:
multiple definition of operator<<(std::ostream&, Complex const&)
I've found that making this function inline solves the problem, but I don't understand why. Why does the compiler complain about multiple definition? My header file is guarded (with #define COMPLEX_H).
And, if complaining about the operator<< function, why not complain about the public real() function, which is defined in the header as well?
And is there another solution besides using the inline keyword?
The problem is that the following piece of code is a definition, not a declaration:
std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
You can either mark the function above and make it "inline" so that multiple translation units may define it:
inline std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
Or you can simply move the original definition of the function to the "complex.cpp" source file.
The compiler does not complain about "real()" because it is implicitly inlined (any member function whose body is given in the class declaration is interpreted as if it had been declared "inline"). The preprocessor guards prevent your header from being included more than once from a single translation unit ("*.cpp" source file"). However, both translation units see the same header file. Basically, the compiler compiles "main.cpp" to "main.o" (including any definitions given in the headers included by "main.cpp"), and the compiler separately compiles "complex.cpp" to "complex.o" (including any definitions given in the headers included by "complex.cpp"). Then the linker merges "main.o" and "complex.o" into a single binary file; it is at this point that the linker finds two definitions for a function of the same name. It is also at this point that the linker attempts to resolve external references (e.g. "main.o" refers to "Complex::Complex" but does not have a definition for that function... the linker locates the definition from "complex.o", and resolves that reference).
Move implementation to complex.cpp
Right now after including this file implementation is being compiled to every file.
Later during linking there's a obvious conflict because of duplicate implementations.
::real() is not reported because it's inline implicitly (implementation inside class definition)
I was having this problem, even after my source and header file were correct.
It turned out Eclipse was using stale artifacts from a previous (failed) build.
To fix, use Project > Clean then rebuild.
An alternative to designating a function definition in a header file as inline is to define it as static. This will also avoid the multiple definition error.

Multiple definition of function (in derived class) [duplicate]

Given this code sample:
complex.h :
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
class Complex
{
public:
Complex(float Real, float Imaginary);
float real() const { return m_Real; };
private:
friend std::ostream& operator<<(std::ostream& o, const Complex& Cplx);
float m_Real;
float m_Imaginary;
};
std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
#endif // COMPLEX_H
complex.cpp :
#include "complex.h"
Complex::Complex(float Real, float Imaginary) {
m_Real = Real;
m_Imaginary = Imaginary;
}
main.cpp :
#include "complex.h"
#include <iostream>
int main()
{
Complex Foo(3.4, 4.5);
std::cout << Foo << "\n";
return 0;
}
When compiling this code, I get the following error:
multiple definition of operator<<(std::ostream&, Complex const&)
I've found that making this function inline solves the problem, but I don't understand why. Why does the compiler complain about multiple definition? My header file is guarded (with #define COMPLEX_H).
And, if complaining about the operator<< function, why not complain about the public real() function, which is defined in the header as well?
And is there another solution besides using the inline keyword?
The problem is that the following piece of code is a definition, not a declaration:
std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
You can either mark the function above and make it "inline" so that multiple translation units may define it:
inline std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
Or you can simply move the original definition of the function to the "complex.cpp" source file.
The compiler does not complain about "real()" because it is implicitly inlined (any member function whose body is given in the class declaration is interpreted as if it had been declared "inline"). The preprocessor guards prevent your header from being included more than once from a single translation unit ("*.cpp" source file"). However, both translation units see the same header file. Basically, the compiler compiles "main.cpp" to "main.o" (including any definitions given in the headers included by "main.cpp"), and the compiler separately compiles "complex.cpp" to "complex.o" (including any definitions given in the headers included by "complex.cpp"). Then the linker merges "main.o" and "complex.o" into a single binary file; it is at this point that the linker finds two definitions for a function of the same name. It is also at this point that the linker attempts to resolve external references (e.g. "main.o" refers to "Complex::Complex" but does not have a definition for that function... the linker locates the definition from "complex.o", and resolves that reference).
Move implementation to complex.cpp
Right now after including this file implementation is being compiled to every file.
Later during linking there's a obvious conflict because of duplicate implementations.
::real() is not reported because it's inline implicitly (implementation inside class definition)
I was having this problem, even after my source and header file were correct.
It turned out Eclipse was using stale artifacts from a previous (failed) build.
To fix, use Project > Clean then rebuild.
An alternative to designating a function definition in a header file as inline is to define it as static. This will also avoid the multiple definition error.

Errors when compiling complex number class in c++

I have code that is supposed to show addition, subtraction, etc. of complex numbers with no input from the user necessary. I have three classes: test.cpp, complex.cpp, and complex.h to run the program, define the constructors and methods, and create a header class respectively. However, when I run my code I get a series of errors that I've been trying to figure out for a while now.
complex.h
//complex class definition
#ifndef COMPLEX_H
#define COMPLEX_H
//class complex
class Complex
{
public:
Complex(); //default no arg constructor
Complex(double a); //one arg constructor
Complex(double a, double b); //two arg constructor
Complex operator+(const Complex &) const; //addition method
Complex operator-(const Complex &) const; //subtraction method
Complex operator*(const Complex &) const; //multiplication method
Complex operator/(const Complex &) const; //division method
void print() const; //output
private:
double a; //real number
double b; //imaginary number
}; //end class Complex
#endif
complex.cpp
#include "stdafx.h"
#include <iostream>
#include "complex.h"
using namespace std;
//no arg constructor
Complex::Complex()
{
a = 0;
b = 0;
}
//one arg instructor
Complex::Complex(double real)
{
a = real;
b = 0;
}
//two arg constructor
Complex::Complex(double real, double imaginary)
{
a = real;
b = imaginary;
}
//addition
Complex Complex::operator+(const Complex &number2) const
{
return a + number2.a, b + number2.b;
}
//subtraction
Complex Complex::operator-(const Complex &number2) const
{
return a - number2.a, b - number2.b;
}
//multiplication
Complex Complex::operator*(const Complex &number2) const
{
return a * number2.a, b * number2.b;
}
//division
Complex Complex::operator/(const Complex &number2) const
{
return a / number2.a, b / number2.b;
}
//output display for complex number
void Complex::print() const
{
cout << '(' << a << ", " << b << ')';
}
test.cpp
#include <iostream>
#include <complex>
#include "complex.h"
#include "stdafx.h"
using namespace std;
int main()
{
Complex b(1.0, 0.0);
Complex c(3.0, -1.0);
/*cout << "a: ";
a.print();
system ("PAUSE");*/
};
In test right now as the code shows the lower parts are commented out and I have attempted to only call two of the three constructors to see if I can get any of this working.
The errors I receive:
error C2065: 'Complex' : undeclared identifier
error C2065: 'Complex' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'b'
error C2146: syntax error : missing ';' before identifier 'c'
error C3861: 'b': identifier not found
error C3861: 'c': identifier not found
I am trying to run this in Visual Studio 2010. Can someone please help?
I think the problem is in your Complex.cpp. If there are errors compiling the code of the class, the class is unknown and so the identifier Complex, too.
After a quick view, everthing seem to be correct but your operator functions have some errors:
Incorrect:
//addition
Complex Complex::operator+(const Complex &number2) const
{
return a + number2.a, b + number2.b;
}
Correct:
//addition
Complex Complex::operator+(const Complex &number2) const
{
return Complex(a + number2.a, b + number2.b);
}
The problems are the return types of your operator methods. They have to be the same as declared. Also you cannot return two variables at the same time. If you want to do so, you have to use structs or classes (by calling the constructor of your class Complex with the new values).
If you want to manipulate the members a and b, you cannot define the methods as "const". If you say a function is const, it means all class members values won't be manipulated by calling this method.
You may facing a problem with stdafx.h issue
At least you should try to put #include "stdafx.h before every other include
Or
you could try to set your project property to not use stdafx.h
1. right click on project and select Properties
2. go to C/C++ -> Precompiled Headers
3. select "Not Using Precompiled Headers"
#include <complex>
#include "complex.h"
That's a bad smell. It's possible that both those #include files use the same name for the header guard.
Try changing yours like this.
#ifndef MY_COMPLEX_H
#define MY_COMPLEX_H
.. And maybe rename your module from "complex" to something else, to avoid clashes with the system header file of the same name.
Try making it as simple as possible. Then add parts back until you find your error.
Does this work?
#include "complex.h"
int main()
{
Complex c(3.0, -1.0);
return 0;
};

multiple definition in header file

Given this code sample:
complex.h :
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
class Complex
{
public:
Complex(float Real, float Imaginary);
float real() const { return m_Real; };
private:
friend std::ostream& operator<<(std::ostream& o, const Complex& Cplx);
float m_Real;
float m_Imaginary;
};
std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
#endif // COMPLEX_H
complex.cpp :
#include "complex.h"
Complex::Complex(float Real, float Imaginary) {
m_Real = Real;
m_Imaginary = Imaginary;
}
main.cpp :
#include "complex.h"
#include <iostream>
int main()
{
Complex Foo(3.4, 4.5);
std::cout << Foo << "\n";
return 0;
}
When compiling this code, I get the following error:
multiple definition of operator<<(std::ostream&, Complex const&)
I've found that making this function inline solves the problem, but I don't understand why. Why does the compiler complain about multiple definition? My header file is guarded (with #define COMPLEX_H).
And, if complaining about the operator<< function, why not complain about the public real() function, which is defined in the header as well?
And is there another solution besides using the inline keyword?
The problem is that the following piece of code is a definition, not a declaration:
std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
You can either mark the function above and make it "inline" so that multiple translation units may define it:
inline std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
Or you can simply move the original definition of the function to the "complex.cpp" source file.
The compiler does not complain about "real()" because it is implicitly inlined (any member function whose body is given in the class declaration is interpreted as if it had been declared "inline"). The preprocessor guards prevent your header from being included more than once from a single translation unit ("*.cpp" source file"). However, both translation units see the same header file. Basically, the compiler compiles "main.cpp" to "main.o" (including any definitions given in the headers included by "main.cpp"), and the compiler separately compiles "complex.cpp" to "complex.o" (including any definitions given in the headers included by "complex.cpp"). Then the linker merges "main.o" and "complex.o" into a single binary file; it is at this point that the linker finds two definitions for a function of the same name. It is also at this point that the linker attempts to resolve external references (e.g. "main.o" refers to "Complex::Complex" but does not have a definition for that function... the linker locates the definition from "complex.o", and resolves that reference).
Move implementation to complex.cpp
Right now after including this file implementation is being compiled to every file.
Later during linking there's a obvious conflict because of duplicate implementations.
::real() is not reported because it's inline implicitly (implementation inside class definition)
I was having this problem, even after my source and header file were correct.
It turned out Eclipse was using stale artifacts from a previous (failed) build.
To fix, use Project > Clean then rebuild.
An alternative to designating a function definition in a header file as inline is to define it as static. This will also avoid the multiple definition error.