GNU GCC编译器错误“;“主”的多重定义; 我是Ubuntu的新手,现在我需要在C++中开发我的作业。 我使用代码块IDE编写C++程序。 每当我编译其中的内容时,它都会出现以下错误: multiple definition of main warning: control reaches end of non-void function
下面是我现在要编译的代码:GNU GCC编译器错误“;“主”的多重定义; 我是Ubuntu的新手,现在我需要在C++中开发我的作业。 我使用代码块IDE编写C++程序。 每当我编译其中的内容时,它都会出现以下错误: multiple definition of main warning: control reaches end of non-void function,c++,gcc,codeblocks,gnu,C++,Gcc,Codeblocks,Gnu,下面是我现在要编译的代码: #include <iostream> #include <stdlib.h> using namespace std; /* The Node class */ class Node { public: int get() { return object; }; void set(int object) { this->object = object; }; Node * getN
#include <iostream>
#include <stdlib.h>
using namespace std;
/* The Node class */
class Node
{
public:
int get() { return object; };
void set(int object) { this->object = object; };
Node * getNext() { return nextNode; };
void setNext(Node * nextNode) { this->nextNode = nextNode; };
private:
int object;
Node * nextNode;
};
/* The List class */
class List
{
public:
List();
void add (int addObject);
int get();
bool next();
friend void traverse(List list);
friend List addNodes();
private:
int size;
Node * headNode;
Node * currentNode;
Node * lastCurrentNode;
};
/* Constructor */
List::List()
{
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
lastCurrentNode = NULL;
size = 0;
}
/* add() class method */
void List::add (int addObject)
{
Node * newNode = new Node();
newNode->set(addObject);
if( currentNode != NULL )
{
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode );
lastCurrentNode = currentNode;
currentNode = newNode;
}
else
{
newNode->setNext(NULL);
headNode->setNext(newNode);
lastCurrentNode = headNode;
currentNode = newNode;
}
size ++;
}
/* get() class method */
int List::get()
{
if (currentNode != NULL)
return currentNode->get();
}
/* next() class method */
bool List::next()
{
if (currentNode == NULL) return false;
lastCurrentNode = currentNode;
currentNode = currentNode->getNext();
if (currentNode == NULL || size == 0)
return false;
else
return true;
}
/* Friend function to traverse linked list */
void traverse(List list)
{
Node* savedCurrentNode = list.currentNode;
list.currentNode = list.headNode;
for(int i = 1; list.next(); i++)
{
cout << "\n Element " << i << " " << list.get();
}
list.currentNode = savedCurrentNode;
}
/* Friend function to add Nodes into the list */
List addNodes()
{
List list;
list.add(2);
list.add(6);
list.add(8);
list.add(7);
list.add(1);
cout << "\n List size = " << list.size <<'\n';
return list;
}
int main()
{
List list = addNodes();
traverse(list);
return 0;
}
#包括
#包括
使用名称空间std;
/*节点类*/
类节点
{
公众:
int get(){return object;};
无效集(int对象){this->object=object;};
Node*getNext(){return nextNode;};
void setNext(Node*nextNode){this->nextNode=nextNode;};
私人:
int对象;
节点*nextNode;
};
/*列表类*/
班级名单
{
公众:
List();
void add(int addObject);
int get();
bool next();
朋友无效遍历(列表);
好友列表addNodes();
私人:
整数大小;
节点*头节点;
节点*当前节点;
节点*lastCurrentNode;
};
/*建造师*/
列表::列表()
{
headNode=新节点();
headNode->setNext(空);
currentNode=NULL;
lastCurrentNode=NULL;
尺寸=0;
}
/*add()类方法*/
无效列表::添加(int addObject)
{
Node*newNode=newNode();
新建节点->设置(添加对象);
if(currentNode!=NULL)
{
newNode->setNext(currentNode->getNext());
currentNode->setNext(新建节点);
lastCurrentNode=currentNode;
currentNode=newNode;
}
其他的
{
新建节点->设置下一步(空);
headNode->setNext(新建节点);
lastCurrentNode=头节点;
currentNode=newNode;
}
大小++;
}
/*get()类方法*/
int List::get()
{
if(currentNode!=NULL)
返回currentNode->get();
}
/*next()类方法*/
bool List::next()
{
if(currentNode==NULL)返回false;
lastCurrentNode=currentNode;
currentNode=currentNode->getNext();
如果(currentNode==NULL | | size==0)
返回false;
其他的
返回true;
}
/*遍历链表的友元函数*/
无效遍历(列表)
{
Node*savedCurrentNode=list.currentNode;
list.currentNode=list.headNode;
for(int i=1;list.next();i++)
{
cout您的IDE似乎不仅仅是编译一个文件,而是另一个包含主函数定义的文件。请查看编译了多少个文件
此外,已编译的IDE将所有警告视为错误(-Werror)或禁用此标志。您的IDE似乎不仅仅编译一个文件,而是另一个包含主函数定义的文件。请查看正在编译的文件数量
此外,您的编译程序正在将所有警告视为错误(-Werror)或禁用此标志。程序可以使用(yourcode.cc包含源代码)进行良好编译:
并调用/yourcode
输出:
List size = 5
Element 1 2
Element 2 6
Element 3 8
Element 4 7
Element 5 1
显然,您的IDE将向链接器添加一些标志。请向我们显示您的编译标志/设置。请参阅编译日志或运行make命令以执行更详细的操作
可以看一看。该程序可以很好地编译(yourcode.cc包含源代码):
并调用/yourcode
输出:
List size = 5
Element 1 2
Element 2 6
Element 3 8
Element 4 7
Element 5 1
显然,您的IDE将向链接器添加一些标志。请向我们显示您的编译标志/设置。请参阅编译日志或运行make命令以执行更详细的操作
可以看一下。问题只是,我的IDE一次编译了多个文件,
在函数int List::get()
中,
我在if语句之后的函数末尾添加了else return-1
,
我的意思是在编辑代码之前,int List::get()
函数是这样的:
int List::get() {
if (currentNode != NULL)
return currentNode->get();
}
我将此替换为:
int List::get() {
if (currentNode != NULL)
return currentNode->get();
else return -1;
}
它工作得很好。问题是,我的IDE一次编译了多个文件,
在函数int List::get()
中,
我在if语句之后的函数末尾添加了else return-1
,
我的意思是在编辑代码之前,int List::get()
函数是这样的:
int List::get() {
if (currentNode != NULL)
return currentNode->get();
}
我将此替换为:
int List::get() {
if (currentNode != NULL)
return currentNode->get();
else return -1;
}
它工作正常。List::get()
有一个控制路径,该路径退出函数而不返回值(即当if
中的条件为false时)。显示编译命令。首先尝试在终端的命令行上使用g++-Wall-Wextra-g
进行编译。下次发布I)代码,而不是腐烂的链接,II)最小测试用例请向您添加参数main
函数:int main(int argc,char*argv[])
@sfrehse:这是可选的。List::get()
有一个退出函数而不返回值的控制路径(即当if
中的条件为false时)。显示编译命令。首先尝试在终端的命令行上使用g++-Wall-Wextra-g
进行编译。下次发布I)代码,而不是腐烂的链接,II)最小测试用例请向您添加参数main
函数:int main(int argc,char*argv[])
@sfrehse:这是可选的。如果int List::get()中的current==NULL,您可以返回-1。现在它只给出了一个错误main的多个定义
:(不要禁用标志。修复警告。问题与您在回答@DídacPérez时所说的相同,IDE一次编译了多个文件,现在我尝试使用单个文件,它工作正常。我很高兴知道您已经解决了问题。您可以将我的答案标记为正确答案吗?如果current==NULL,您可以返回-1。)在int List::get()中尝试了它,现在它只给出了一个错误main的多个定义
:(不要禁用该标志。修复警告。问题与您在回答@DídacPérez中所说的一样,IDE一次编译了多个文件,现在我尝试使用单个文件,结果正确