issue with classes and vectors!

I've been trying to make a program, using classes and vectors, and I keep running into this error, or I guess, errors.

"Severity Code Description Project File Line Suppression State
Error LNK2005 "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > filePath" (?filePath@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in Source.obj FinalProjectEver C:\Users\Bruno***\source\repos\FinalProjectEver\FinalProjectEver\Voter.obj 1

Severity Code Description Project File Line Suppression State
Error LNK2005 "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > nameOfFile" (?nameOfFile@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in Source.obj FinalProjectEver C:\Users\Brunk53350\source\repos\FinalProjectEver\FinalProjectEver\Voter.obj 1

Severity Code Description Project File Line Suppression State
Error LNK2005 "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl FileLoader(void)" (?FileLoader@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in Source.obj FinalProjectEver C:\Users\Brunk53350\source\repos\FinalProjectEver\FinalProjectEver\Voter.obj 1"

basically its just twenty of those. This is an error I've ran into in MULTIPLE programs I've had. Here are some bits of my code for reference



My header
1
2
3
4
5
6
7
8
9
10
class Voter {
private:
	string firstName;
	string lastName;
	string state;
	int zipCode;
	string party;
public:
	void setVoter(string, string, string, int, string);
	void printVoter();



How I have it called:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Voter.h"

void Voter::setVoter(string first, string last, string staet, int zip, string partia) {
	
	firstName = first;
	lastName = last;
	state = staet;
	zipCode = zip;
	party = partia;

}

void Voter::printVoter() {
	cout << "Voter first name: " << firstName << endl;
	cout << "Voter last name: " << lastName << endl;
	cout << "Voters state: " << state << endl;
	cout << "Voters zip-code: " << zipCode << endl;
	cout << "Voters registered party: " << party << endl;
}


bits from how its called in my main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vector<Voter>votersVect(1, voterAdder());

	while (yesNo == 'y') {
		int choice = menu();
		switch (choice) {
		case 1:
			votersVect.push_back(voterAdder());
			totalVoters++;
			cout << "Votes: " << totalVoters << endl;
			break;
		case 2:

			for (int i = 0; i < totalVoters; i++)
			{
				votersVect[i].printVoter();
			}
			
			break;


you've got a linker error
the prroblem is with your project configuration and build command

(or you #include a .cpp, don't do that)


Also, the errors refer to filePath, nameOfFile, FileLoader
none of which appear in the code you've posted
Hello AshleyHideo1917,

As ne555 has pointed out this is a time when it is best to post the whole code and not what you think is causing the problem.

It is also a good idea to mention what IDE you are using.

Time and experience has taught me that the line number of an error message may not be where it started. And a linker error like this is even harder to understand.

In the error message:

"Severity Code Description Project File Line Suppression State
Error LNK2005 "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > filePath" (?filePath@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
already defined in Source.obj FinalProjectEver C:\Users\Bruno***\source\repos\FinalProjectEver\FinalProjectEver\Voter.obj 1


"filePath" is the first useful information here. The second part in bold ending with "Voter.obj" and the line number is always "1". Tells me that a ".cpp" file other than "main" was compiled first and somehow in m"main" you are trying to define these variables again.

By the error message and the path it shows it looks like you are using some version of VS (Visual Studio). Not that is will make a lot of difference, but it does help to know which VS20?? it is.

To see the whole code and understand what ".cpp" files are part of the project will help to resolve the linker error.

Hope that helps,

Andy
figured it out! (kinda)

Basically, something about my PersonalLibrary the program didn't like, I wanna thank ne555 for point that out with the errors!

filePath and nameOfFile are global variables in my personal library from a long time ago, and when I looked back I realized that ALL of my errors had something to do with my personal library.

As of now I have no idea what it is about my personal library and classes that don't get along, but I'll just include what i need in my program for now. Merci beaucoup!
Topic archived. No new replies allowed.