C++ 已在 main.obj 中定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13128719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Already defined in main.obj
提问by laura
This is the code for my problem and i get 4 errors:
这是我的问题的代码,我收到 4 个错误:
- student.obj : error LNK2005: "struct Node * admitedFirstNode" (?admitedFirstNode@@3PAUNode@@A) already defined in main.obj
- student.obj : error LNK2005: "struct Node * allFirstNode" (?allFirstNode@@3PAUNode@@A) already defined in main.obj
- student.obj : error LNK2005: "struct Node * rejectedFirstNode" (?rejectedFirstNode@@3PAUNode@@A) already defined in main.obj
- pb4_OOP_lab1\Debug\pb4_OOP_lab1.exe : fatal error LNK1169: one or more multiply defined symbols found
- student.obj : error LNK2005: "struct Node * allowedFirstNode" (?admitedFirstNode@@3PAUNode@@A) 已经在 main.obj 中定义
- student.obj : error LNK2005: "struct Node * allFirstNode" (?allFirstNode@@3PAUNode@@A) 已经在 main.obj 中定义
- student.obj : error LNK2005: "struct Node * deniedFirstNode" (?rejectedFirstNode@@3PAUNode@@A) 已经在 main.obj 中定义
- 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();
回答by Luchian Grigore
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
Node * rejectedFirstNode;
头文件中的定义会导致多重定义的符号,因为包含该头文件的所有翻译单元都会为其生成一个符号。相反,在标题中,有
//students.h
extern Node * rejectedFirstNode;
and move the definition in a single cpp file:
并将定义移动到单个 cpp 文件中:
//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.
您似乎也在编写 C 代码。为什么这个标记为 C++?如果您不了解 C++ 所提供的所有内容,请阅读一本很好的介绍性书籍。
回答by gogoprog
You are declaring variables in students.h and students.h is included in both main.cpp and student.cpp.
您在 student.h 中声明变量,而 student.h 包含在 main.cpp 和 student.cpp 中。
You should avoid declaring variables in headers file.
您应该避免在头文件中声明变量。
Try to put following code into students.cpp :
尝试将以下代码放入 Students.cpp 中:
Node *allFirstNode;
Node *admitedFirstNode;
Node *rejectedFirstNode;
回答by Olaf Dietsche
You have admitedFirstNode
, allFirstNode
and rejectedFirstNode
defined in the header file. This defines it in every cpp, that includes the "students.h" file.
你有admitedFirstNode
,allFirstNode
并rejectedFirstNode
在头文件中定义。这在每个 cpp 中定义了它,包括“students.h”文件。
Split the declaration and definition. Declare them in "students.h":
拆分声明和定义。在“students.h”中声明它们:
extern Node *allFirstNode;
extern Node *admitedFirstNode;
extern Node *rejectedFirstNode;
and in "students.cpp" define the variables:
并在“students.cpp”中定义变量:
Node *allFirstNode;
Node *admitedFirstNode;
Node *rejectedFirstNode;