GNU GCC编译器错误“ main的多个定义” [英] GNU GCC compilor error "multiple definition of main"

查看:131
本文介绍了GNU GCC编译器错误“ main的多个定义”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ubuntu的新手,现在我需要用C ++开发我的作业。
我正在使用代码块IDE编写c ++程序。
每当我在其中进行编译时,都会出现以下错误:

I am new to ubuntu, now I need to develop my assignment in C++. I am using codeblocks IDE to write c++ programs. Whenever I compile something in it, it gives these errors:

multiple definition of main
warning: control reaches end of non-void function

这是我现在要编译的代码:

Here is the code I want to compile for now:

#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;
}

有人可以解释,我在哪里搞砸?

Can anyone explain, where do I messing up?

推荐答案

问题只是,我的IDE一次编译了多个文件,
也在函数 int列表中:: get()
在if语句$ b之后,我必须在此函数的末尾添加 else return -1 $ b

我的意思是在编辑代码之前, int List :: get()函数是这样的:

The Problem was just, my IDE was compiling multiple files at once, and also in the function int List::get() , I got to add else return -1 at the end of this function after if statement,
I mean before editing my code the int List::get() function was like this:

int List::get() {
    if (currentNode != NULL)
        return currentNode->get();
}

我将其替换为:

int List::get() {
    if (currentNode != NULL)
        return currentNode->get();
    else return -1;
}

,效果很好。

这篇关于GNU GCC编译器错误“ main的多个定义”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆