8

This is the code for my problem and i get 4 errors:

  1. student.obj : error LNK2005: "struct Node * admitedFirstNode" (?admitedFirstNode@@3PAUNode@@A) already defined in main.obj
  2. student.obj : error LNK2005: "struct Node * allFirstNode" (?allFirstNode@@3PAUNode@@A) already defined in main.obj
  3. student.obj : error LNK2005: "struct Node * rejectedFirstNode" (?rejectedFirstNode@@3PAUNode@@A) already defined in main.obj
  4. pb4_OOP_lab1\Debug\pb4_OOP_lab1.exe : fatal error LNK1169: one or more multiply defined symbols found
#include "students.h"                          //main 
int main()                                         
{
for(int i=0;i<NR_STUDENTS;i++)
{
    Student *s1=new Student;
    cout<<"Enter name: ";
    getline(cin,s1->name);
    cout<<"Enter garde: ";
    cin>>s1->grade;
    cin.ignore();
    addStudent(s1);
    delete s1;
}

}
#include "students.h"                           //students.cpp

void AddNodeToList(Node *firstNode, Student *studToAdd)
{
Node *nodeToAdd=new Node;
nodeToAdd->student=studToAdd;
nodeToAdd->next=NULL;

if(firstNode==NULL)
{
    firstNode=nodeToAdd;
}
else
{
    Node *temp;
    temp=firstNode;
    while(temp->next != NULL)
    {
        temp=temp->next;
    }
    temp->next=nodeToAdd;
}
}
void addStudent(Student *studentToAdd)
{
AddNodeToList(allFirstNode,studentToAdd);
}

void split()
{
Node *temp=allFirstNode;
while(temp->next != NULL)
{
    Student *currentStud=temp->student;
    if(currentStud->grade < GR)
    {
        AddNodeToList(rejectedFirstNode,currentStud);
    }
    else    
    {
        AddNodeToList(admitedFirstNode,currentStud);
    }
}
}

void print(Node *firstNode)
{

if(firstNode==NULL)
{
    return;
}
else
{
    Node *temp=firstNode;
    Student *current=temp->student;
    while(temp->next != NULL)
    {
        cout<<"----------------------------------------------"<<endl;
        cout<<"Student name: "<<current->name<<endl;
        cout<<"Student grade: "<<current->grade<<endl;
    }
}
}

#include <iostream>                       //students.h
#include <string>

using namespace std;

const int NR_STUDENTS=5;
const double GR=5.0;

struct Student
{
string name;
double grade;
};

struct Node
{
Student *student;
Node *next;
};

Node *allFirstNode;
Node *admitedFirstNode;
Node *rejectedFirstNode;

void addStudent(Student *studentToAdd);
void split();
void sort();
void print();
Servy
  • 193,745
  • 23
  • 295
  • 406
laura
  • 1,995
  • 7
  • 34
  • 63
  • 2
    For posterity, if the other suggestions don't solve your problem, I have gotten this error before because I accidentally did #include "MyFile.cpp" instead of #include "MyFile.h". Changing it to .h solved my problem. Worth checking your most recent changes to see if you did that by accident. – sitting-duck Sep 22 '16 at 16:48

3 Answers3

15

The definition Node * rejectedFirstNode; in a header file leads to a multiply defined symbol because all translation units that include that header will generate a symbol for it. Instead, in the header, have

//students.h
extern Node * rejectedFirstNode;

and move the definition in a single cpp file:

//students.cpp
Node * rejectedFirstNode;

It also seems like you're writing C code. Why is this tagged C++? If you're unaware of all C++ has to offer, read a good introductory book.

Community
  • 1
  • 1
Luchian Grigore
  • 236,802
  • 53
  • 428
  • 594
3

You are declaring variables in students.h and students.h is included in both main.cpp and student.cpp.

You should avoid declaring variables in headers file.

Try to put following code into students.cpp :

Node *allFirstNode;
Node *admitedFirstNode;
Node *rejectedFirstNode;
gogoprog
  • 523
  • 2
  • 7
1

You have admitedFirstNode, allFirstNode and rejectedFirstNode defined in the header file. This defines it in every cpp, that includes the "students.h" file.

Split the declaration and definition. Declare them in "students.h":

extern Node *allFirstNode;
extern Node *admitedFirstNode;
extern Node *rejectedFirstNode;

and in "students.cpp" define the variables:

Node *allFirstNode;
Node *admitedFirstNode;
Node *rejectedFirstNode;
Olaf Dietsche
  • 66,104
  • 6
  • 91
  • 177