Having trouble with [LNK1169 one or more multiply defined symbols found]

This works when combined into a single file, but when I try to split it into main.cpp, BigInt.cpp, and BigInt.h it comes up with the following errors. Any idea how to fix this?


1
2
3
LNK2005	"class BigInt __cdecl operator*(class BigInt &,class BigInt &)" (??D@YA?AVBigInt@@AAV0@0@Z) already defined in BigInt.obj
Error	LNK2005	"class BigInt __cdecl operator+(class BigInt &,class BigInt &)" (??H@YA?AVBigInt@@AAV0@0@Z) already defined in BigInt.obj
Error	LNK1169	one or more multiply defined symbols found




main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "BigInt.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

int main() {

	BigInt B1("2"); 
	BigInt B2("6");
	BigInt B4 = /*B1 + B2;*/ pow(B1, B2);//  B1 * B2;

	std::cout << std::endl << std::endl;
	std::cout << "B1^B2 is " << B4 << std::endl;

	std::cin.get();
	return 0;
}


BigInt.h
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
#pragma once
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

class BigInt {
	std::vector<int> aBigInt;
public:
	BigInt(const BigInt& a);
	BigInt(std::string = 0);

	BigInt(int); // for pow counter
	BigInt(std::vector<int>);
	BigInt& operator+=(BigInt&);
	BigInt& operator*=(BigInt &);
	BigInt& operator|(BigInt&);
	std::vector<int>getBigInt() const;
	void setBigInt(std::vector<int>);
	void push_back(int);
	void clear();
	void removeLeadZeroes();
	void removeTrailZeroes();
};

bool operator!=(BigInt&, BigInt &);
bool operator==(BigInt&, BigInt &);
BigInt operator+(BigInt&, BigInt&);
BigInt operator*(BigInt&, BigInt&);
BigInt operator ^(BigInt&, BigInt&);
BigInt pow(BigInt&, BigInt&);
BigInt operator+(BigInt &lhs, BigInt &rhs) { return lhs += rhs; }
BigInt operator*(BigInt &lhs, BigInt &rhs) { return lhs *= rhs; }
std::ostream& operator<<(std::ostream& out, BigInt a);
int stringToInt(std::string);


BigInt.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "BigInt.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>


void BigInt::removeLeadZeroes()
{
	for (auto x : aBigInt)
	{
		while (x == 0) { aBigInt.pop_back(); }
		if (x != 0) { break; }
	}
}

void BigInt::removeTrailZeroes()
{
	while (!aBigInt.empty() && aBigInt[aBigInt.size() - 1] == 0)
	{
		aBigInt.pop_back();
	}
}

void removeLeadZero(std::vector<int> &x)
{
	for (auto y : x)
	{
		while (y == 0) { x.pop_back(); }
		if (y != 0) { break; }
	}
}

void removeTrailZero(std::vector<int> &x)
{
	while (!x.empty() && x[x.size() - 1] == 0)
	{
		x.pop_back();
	}
}

std::vector<int> BigInt::getBigInt() const { return aBigInt; }

void BigInt::setBigInt(std::vector<int> x)
{
	aBigInt = x;
}

void BigInt::push_back(int x)
{
	aBigInt.push_back(x);
}

void BigInt::clear()
{
	aBigInt.clear();
}

BigInt::BigInt(const BigInt& other) :aBigInt(other.aBigInt) {}

/*(BigInt& BigInt::operator=(BigInt a) {
this->aBigInt.swap(a.aBigInt);
return *this;
}*/

/*int stringToInt(std::string x)
{
return stoi(x);
}*/

BigInt::BigInt(std::string num) {
	std::vector<int> vec;
	for (auto x : num) { vec.push_back(x - '0'); }
	for (auto x : vec) { aBigInt.push_back(x); }

	//for (auto x : aBigInt) { std::cout << x; }
	/*std::transform(num.rbegin(), num.rend(), std::back_inserter(aBigInt),
	[](char c) { return (int)c - '0'; });*/
	//removeTrailZeroes();
	for (auto x : num) { std::cout << x; } std::cout << " ";
}

BigInt::BigInt(std::vector<int> v)
{
	aBigInt = v;
}

BigInt::BigInt(int x)
{
	std::vector<int> v{ x };
	aBigInt = v;  
}

BigInt pow(BigInt &lhs, BigInt &rhs)
{
	std::cout << std::endl << std::endl;

	if (rhs == BigInt(0)) { return BigInt(1); }

	BigInt main(1);
	BigInt copy(lhs.getBigInt());

	std::vector<int> temp;
	std::vector<int> toReverse;

	BigInt counter(0);
	BigInt incrementer(1);

	int testtotal = 1;
	int test = 2;

	std::cout << std::endl;
	while (counter != rhs)
	{
		counter += incrementer;
		main | copy;
		std::cout << main << "--";
	}
	return main;
}

BigInt operator ^(BigInt &lhs, BigInt &rhs)
{

	BigInt copy(lhs.getBigInt());


	BigInt counter(-1); // offset counter by 1
	BigInt incrementer("1");

	while (counter != rhs)
	{
		//std::cout << "<counter: " << counter << " > ";
		//std::cout << "<copy: " << copy << " > ";
		counter += incrementer;

		lhs *= copy;
		std::cout << "x";
	}
	std::cout << lhs << "<THIS>";
	return lhs;
}

bool operator!=(BigInt& lhsBigInt, BigInt & rhsBigInt)
{

	///std::cout << lhsBigInt << "LHS" << std::endl << rhsBigInt << "rhs";
	for (const auto &x : lhsBigInt.getBigInt())
	{
		for (const auto &y : rhsBigInt.getBigInt())
		{
			if (x != y) { return true; }
		}
	}
	return false;
}

bool operator==(BigInt &lhs, BigInt &rhs)
{
	if (lhs != rhs) { return false; }
	return true;
}


BigInt& BigInt::operator *=(BigInt& rhsBigInt)
{

	std::vector<int> zeroCheck = { 0 };
	if (aBigInt == zeroCheck || rhsBigInt.aBigInt == zeroCheck)
	{
		aBigInt = { 0 };
		return *this;
	}

	int lhsSize = aBigInt.size();
	int rhsSize = rhsBigInt.aBigInt.size();
	std::vector<int> product(aBigInt.size() + rhsBigInt.aBigInt.size()); // ,0
	auto lhsIndex = 0;
	auto rhsIndex = 0;
	//for (auto i = 0; i < lhsSize; ++i) // l -> r
	for (auto i = lhsSize - 1; i >= 0; --i) // r -> l
	{
		int overflow = 0;
		int lhsDigit = aBigInt[i];
		rhsIndex = 0; // < shift each result

					  //for (auto i = 0; i < rhsSize; ++i) // l -> r
		for (auto i = rhsSize - 1; i >= 0; --i) // r -> l
		{
			int rhsDigit = rhsBigInt.aBigInt[i];// -'0';
			int sum = lhsDigit * rhsDigit + product[lhsIndex + rhsIndex] + overflow;
			overflow = sum / 10;
			product[lhsIndex + rhsIndex] = sum % 10;
			++rhsIndex;
		}

		if (overflow > 0) { product[lhsIndex + rhsIndex] += overflow; }
		++lhsIndex;
	}

	this->aBigInt = product;
	this->removeTrailZeroes();  // removes leading zeroes
	return *this;
}

BigInt& BigInt::operator |(BigInt& rhsBigInt)
{

	std::vector<int> zeroCheck = { 0 };
	if (aBigInt == zeroCheck || rhsBigInt.aBigInt == zeroCheck)
	{
		aBigInt = { 0 };
		return *this;
	}

	int lhsSize = aBigInt.size();
	int rhsSize = rhsBigInt.aBigInt.size();
	std::vector<int> product(aBigInt.size() + rhsBigInt.aBigInt.size()); // ,0
	auto lhsIndex = 0;
	auto rhsIndex = 0;
	for (auto i = 0; i < lhsSize; ++i) // l -> r
									   //for (auto i = lhsSize - 1; i >= 0; --i) // r -> l
	{
		int overflow = 0;
		int lhsDigit = aBigInt[i];
		rhsIndex = 0; // < shift each result

		for (auto i = 0; i < rhsSize; ++i) // l -> r
										   //for (auto i = rhsSize - 1; i >= 0; --i) // r -> l
		{
			int rhsDigit = rhsBigInt.aBigInt[i];// -'0';
			int sum = lhsDigit * rhsDigit + product[lhsIndex + rhsIndex] + overflow;
			overflow = sum / 10;
			product[lhsIndex + rhsIndex] = sum % 10;
			++rhsIndex;
		}

		if (overflow > 0) { product[lhsIndex + rhsIndex] += overflow; }
		++lhsIndex;
	}

	this->aBigInt = product;
	this->removeTrailZeroes();  // removes leading zeroes
	return *this;
}

BigInt& BigInt::operator +=(BigInt& rhs)
{
	auto lhsCopy = aBigInt;
	std::vector<int> zeroes(int(rhs.aBigInt.size() - lhsCopy.size()));
	lhsCopy.insert(lhsCopy.begin(), zeroes.begin(), zeroes.end());

	std::vector<int> sum(std::max(rhs.aBigInt.size(), lhsCopy.size()) + 1);
	int overflow = 0;

	std::transform(rhs.aBigInt.crbegin(), rhs.aBigInt.crend(), lhsCopy.crbegin(), sum.rbegin(),
		[&overflow](const int x, const int y)
	{
		int sum = x + y + overflow;
		overflow = sum / 10;
		return sum % 10;
	});

	sum[0] = overflow;
	aBigInt.clear();
	aBigInt.insert(aBigInt.end(), sum.rbegin(), sum.rend());
	this->removeTrailZeroes();
	return *this;
}

std::ostream& operator <<(std::ostream& out, BigInt a)
{
	auto vec = a.getBigInt();
	std::reverse(vec.begin(), vec.end());
	for (auto x : vec) { out << x; }
	return out;
}
Last edited on
You have
1
2
BigInt operator+(BigInt &lhs, BigInt &rhs) { return lhs += rhs; }
BigInt operator*(BigInt &lhs, BigInt &rhs) { return lhs *= rhs; }

in the header. You include the header in both translation units. That is why linker gets them more than once.


You would get the same "multiply defined symbols" error for this pair:
foo.cpp
1
2
3
void foo()
{
}

bar.cpp
1
2
3
4
5
6
7
8
void foo()
{
}

int main()
{
  foo();
}



Move the implementations into one of the source files and leave only declaration into the header, just like you do with the other functions.

Alternatively, use the inline specifier: https://en.cppreference.com/w/cpp/language/inline



EDIT:
What have you here?
1
2
3
BigInt operator+(BigInt &lhs, BigInt &rhs) { return lhs += rhs; }

foo = fubar + gaz;

Lets (pseudo)write the same without function:
1
2
3
4
BigInt &lhs = fubar;
BigInt &rhs = gaz;
lhs += rhs; // means: fubar += gaz
foo = lhs; // means: foo = fubar 


Your foo+bar modifies the foo!!
Furhermore, you cannot call it with these:
1
2
3
const BigInt foo;
const BigInt bar;
foo + bar; // error: foo is const, error: bar is const 

Is that your intention?
It differs from int answer = 35 + 7;, where 35 does not change, and both 35 and 7 are const.
Last edited on
I completely overlooked that - Got it now - thank you very much!
Topic archived. No new replies allowed.