LNK2005 error when trying to overload an operator

Hi,
So I am getting a LNK2005 error when i try and compile this, which showed up when I added the overload of == and != operators in my class. I looked this error up and it has something to do with the compiler thinking this class is already defined, i'm going to guess that it has something to do with the comparing operators already being defined by default?

PolyNumber.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once

class PolyNumber
{
public:
	PolyNumber();
	PolyNumber(int set_coefficent, int set_degree);
	void setDegree(int set);
	void setCoefficient(int set);
	int getDegree();
	int getCoefficient();
	friend bool operator== (const PolyNumber& p1, const PolyNumber& p2);
	friend bool operator!= (const PolyNumber& p1, const PolyNumber& p2);

private:
	int degree;
	int coefficient;
};



PolyNumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once
 #include "PolyNumber.h"


PolyNumber::PolyNumber()
{
	coefficient = 0;
	degree = 0;
}
PolyNumber::PolyNumber(int set_coefficent, int set_degree)
{
	coefficient = set_coefficent;
	degree = set_degree;
}

void PolyNumber::setDegree(int set)
{
	degree = set;
}
void PolyNumber::setCoefficient(int set)
{
	coefficient = set;
}
inline int PolyNumber::getDegree()
{
	return degree;
}
inline int PolyNumber::getCoefficient()
{
	return coefficient;
}
 bool  operator== (const PolyNumber& p1, const PolyNumber& p2)
{
	return (p1.coefficient == p2.coefficient && p1.degree == p2.degree);
}

bool operator!= (const PolyNumber& p1, const PolyNumber& p2)
{
	return !(p1 == p2);
}

main
1
2
3
4
5
6
7
8
9
10
11
12
#include "PolyNumber.h"
#include <iostream>
#include <string>

int main()
{
	PolyNumber test1 = { 5, 6 };
	PolyNumber test2 = { 5, 6 };

	bool isSame = test1 == test2;
	std::cout << isSame;
}

PolyNumber:: missing on those?
I was suspicious of the inline keywords you're using, but it compiles on GCC.
What is the exact, full error message?

Also, probably not related, but you shouldn't have #pragma once in the .cpp file.

jonnin, those are friend functions, so they're not part of the class itself.
Last edited on
Ganado, I would post the rest of my code by its too long and the forum wont let me post. But basically i'm using the PolyNumber as a type in another class, i get this error:
[b] Severity Code Description Project File Line Suppression State
Error LNK2005 "public: __thiscall Polynomial::Polynomial(class Polynomial &)" (??0Polynomial@@QAE@AAV0@@Z) already defined in Polynomial.obj Project11 C:\Users\James\source\repos\Project11\Source.obj 1
[b]

where Polynomial is the class i'm am using PolyNumber type with

the code for that file is as follows (please ignore some code that's not used, as this was a project that was to be adjusted and some of the old stuff is still there):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#pragma once
#include "PolynomialInterface.h"
#include "LinkedBag.cpp"
#include "PolyNumber.h"
static const int POLYNOMIAL_SIZE = 10;

class Polynomial : public Polynomoal_Interface
{
public:
	//Cunstructs am empty Polynomial  
	Polynomial();

	//Copy constructor
	Polynomial(Polynomial& copy);

	/** Cunstructs a Polynomial with a client defined Polynomial
	@param an array of non-negative integer coeffient that does not exceed POLYNOMIAL_SIZE, each coeffient in the array has a power that correspounds
	to the respective value of the location of the ceffient in that array. */
	Polynomial(int coeffient[POLYNOMIAL_SIZE], int size);

	int degree();
	int coefficient(int power);
	bool changeCoefficient(int newCoefficient, int power);
private:
	//static const int POLYNOMIAL_SIZE = 10;
	//int polynomial[POLYNOMIAL_SIZE];
	LinkedBag<PolyNumber> bag;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "Polynomial.h"


Polynomial::Polynomial()
{

}
Polynomial::Polynomial(Polynomial& copy)
{
	std::vector<PolyNumber> copyFrom = copy.bag.toVector();
	for (int i = 0; i < copyFrom.size(); i++)
	{
		bag.add(copyFrom[i]);
	}
}
Polynomial::Polynomial(int coeffient[POLYNOMIAL_SIZE], int size)
{
	for (int i = 0; i <= size; i++)
	{
		PolyNumber number = { coeffient[i], i + 1 };
		bag.add(number);
	}
}
int Polynomial::degree()
{
	int max = 0;
	std::vector<PolyNumber> result = bag.toVector();
	for (int i = 0; i < result.size(); i++)
	{
		if (result[i].getDegree() > max)
		{
			max = result[i].getDegree();
		}
	}
	return max;
}
int Polynomial::coefficient(int power)
{
	int result = 0;
	std::vector<PolyNumber> powerOf = bag.toVector();
	for (int i = 0; i < powerOf.size(); i++)
	{
		if (powerOf[i].getDegree() == power)
		{
			result = powerOf[i].getCoefficient();
		}
	}
	return result;
}
bool Polynomial::changeCoefficient(int newCoefficient, int power)
{
	PolyNumber number = { newCoefficient, power };
	int result = coefficient(power) + newCoefficient;
	bag.remove(number);
	number.setCoefficient(result);
	bag.add(number);
	return true;
}
Last edited on
I can almost guarantee that unless you're working with templates and know exactly what you're doing, #including a .cpp file is what is currently causing at least one of your issues.

You should be including header files, not implementation files.

If you copied your files to a new project and ripped out all functions/classes that don't contribute towards the error, you'd probably gain a better understanding of where the issue is.
Last edited on
...
Last edited on
The official writeup for the error is at https://docs.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-error-lnk2005?view=msvc-160

If you can't understand it and can't go through the possibilities that presents with the code you have written then it might be time to have a rethink about the way you have planned and written your code. Nobody here is likely to find it without the complete code. Good luck with that possibility :)

Your main() and PolyNumber run so that's a plus as said before.

Take the advice you have already been given:
1. get rid of all the comments and clean it up so you can read it!
2. get rid of the inline's and #pragmas they're not important to the debugging.
3. work out which one of the two operators caused the problem - comment out each one at a time. That might go a somewhere towards finding out where the duplicate definitions are.

In terms of the rethink it is clear to me that you need to 'decouple' this LinkedBag<PolyNumber> from the scene and put it into an independent and properly templated class. Either that or come up with some sort of hare-brained inheritance scheme.

As for the class PolynomialInterFace, well that is an 'interesting' name that could possibly be better as just a simple driver. I wouldn't be surprised if that has something to do with the error because I bet it hasn't been unit-tested and and overlap/duplication could easily be there because you have unwanted 'coupling' :(

Classes are generally meant to be independent stand-alone encapsulations of objects. If they don't stand-alone and they aren't unit-tested ...


Topic archived. No new replies allowed.