error LNK2005: Structure already defined

Nov 18, 2014 at 7:01am
I tried to hold off and not rely on outside sources, but after looking over this program for an hour I simply cannot comprehend why it's feeding me back these errors. As of the moment I'm trying to create a void function containing instructions to display data in a file outside my main.cpp file. However, it keeps saying that my main.cpp file has this function already defined (I use the function in main, but it's merely calling it, not defining it).

errors
error LNK2005: "class std::basic_ifstream<char,struct std::char_traits<char> > inFile" (?inFile@@3V?$basic_ifstream@DU?$char_traits@D@std@@@std@@A) already defined in Record Structures Main.obj
error LNK2005: "struct gRec * students" (?students@@3PAUgRec@@A) already defined in Record Structures Main.obj
error LNK1169: one or more multiply defined symbols found

To give more background information, originally I had the void function in function.cpp incorporated into main.cpp, this worked perfectly. Unfortunately I need the void function to be in a separate file, while it doesn't seem to want to be.

header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//libraries
using namespace std;
#include <iomanip>
#include <iostream>
#include <fstream> 
#include <string>
#include <vector>

//import
ifstream inFile("Student Data.txt");

//declarations and structures
const int gSIZE = 4;
const int sSIZE = 5;
struct gRec {
	string name;
	int studID;
	int grades[gSIZE];
	int avgscore;
	char lettergrade;
} students[sSIZE];
void displayrecord(gRec record);


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Header.h"
int main()
{

	for (int k = 0; k != 5; k++)
	{
		string str;
		getline(inFile, students[k].name);
		inFile >> students[k].studID;
		for (int i = 0; i != 4; i++)  
		{
			inFile >> students[k].grades[i];
		}
		getline(inFile, str);
		displayrecord(students[k]);
	}
	system("pause");
}


functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Header.h"
void displayrecord(gRec record)
{
	cout << record.name << endl;
	cout << record.studID << endl;
	//for (int n = 0; n != 4; n++)      
	//record.grades[n];
	cout << record.grades[0] << " ";
	cout << record.grades[1] << " ";
	cout << record.grades[2] << " ";
	cout << record.grades[3];
	cout << endl;
}
Nov 18, 2014 at 7:19am
Last edited on Nov 18, 2014 at 7:20am
Nov 18, 2014 at 7:28am
Don't define variables in header files.

header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef HEADER_H_
#define HEADER_H_

#include <string>

const int gSIZE = 4;

struct gRec {       // surely you can think of a better type name.
    std::string name;
    int studID;
    int grades[gSIZE];
    int avgscore;
    char lettergrade;
};

void displayrecord(gRec);

#endif 


main.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
#include "header.h"
#include <string>
#include <iostream>
#include <fstream>

int main()
{
    const int sSIZE = 5; 
    gRec students[sSIZE];

    std::ifstream inFile("Student Data.txt");

    for (int k = 0; k != sSIZE; k++)
    {
        getline(inFile, students[k].name);
        inFile >> students[k].studID;

        for (int i = 0; i != gSIZE; i++)
        {
            inFile >> students[k].grades[i];
        }

        std::string str;
        getline(inFile, str);
        displayrecord(students[k]);
    }

    system("pause");
}


functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Header.h"
#include <iostream>

void displayrecord(gRec record)
{
    using std::cout;
    using std::endl;

    cout << record.name << endl;
    cout << record.studID << endl;
    //for (int n = 0; n != 4; n++)      
    //record.grades[n];
    cout << record.grades[0] << " ";
    cout << record.grades[1] << " ";
    cout << record.grades[2] << " ";
    cout << record.grades[3];
    cout << endl;
}
Nov 18, 2014 at 11:00am
Thank both of you, I've never heard of gaurds before.

Also nice code formatting Cire, I'll have to take note of that for when I code from now on. Learning code on my own has left me to some pretty nasty habits it seems.
Last edited on Nov 18, 2014 at 11:10am
Topic archived. No new replies allowed.