已在main.obj中定义

lau*_*ura 6 c++ visual-c++

这是我的问题的代码,我得到4个错误:

  1. student.obj:错误LNK2005:"struct Node*admitedFirstNode"(?admitedFirstNode @@ 3PAUNode @@ A)已在main.obj中定义
  2. student.obj:错误LNK2005:已在main.obj中定义的"struct Node*allFirstNode"(?allFirstNode @@ 3PAUNode @@ A)
  3. student.obj:错误LNK2005:已在main.obj中定义的"struct Node*rejectedFirstNode"(?rejectedFirstNode @@ 3PAUNode @@ A)
  4. pb4_OOP_lab1\Debug\pb4_OOP_lab1.exe:致命错误LNK1169:找到一个或多个多重定义的符号
#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();
Run Code Online (Sandbox Code Playgroud)

Luc*_*ore 13

Node * rejectedFirstNode;头文件中的定义导致多重定义的符号,因为包含该头的所有转换单元将为其生成符号.相反,在标题中,有

//students.h
extern Node * rejectedFirstNode;
Run Code Online (Sandbox Code Playgroud)

并在单个cpp文件中移动定义:

//students.cpp
Node * rejectedFirstNode;
Run Code Online (Sandbox Code Playgroud)

你好像在编写C代码.为什么这个标记的C++?如果您不知道所有C++必须提供的内容,请阅读一本好的入门书.