【问题标题】:GNU GCC compilor error "multiple definition of main"GNU GCC 编译器错误“main 的多个定义”
【发布时间】:2014-10-29 08:57:00
【问题描述】:

我是 ubuntu 新手,现在我需要用 C++ 开发我的作业。 我正在使用 codeblocks IDE 编写 c++ 程序。 每当我在其中编译某些东西时,它都会出现以下错误:

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

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

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

谁能解释一下,我在哪里搞砸了?

【问题讨论】:

  • List::get() 有一个控制路径退出函数而不返回值(即当if 中的条件为假时)。
  • 显示你的编译命令。首先尝试在终端的命令行上编译,使用g++ -Wall -Wextra -g
  • 下次发布 I) 代码,而不是烂链接,II) 最小测试用例
  • 请给你加参数main函数:int main ( int argc, char* argv[] )
  • @sfrehse:这是可选的。

标签: c++ gcc codeblocks gnu


【解决方案1】:

您的 IDE 似乎不仅仅是编译一个文件,而是另一个包含主函数定义的文件。请查看正在编译的文件数量。

此外,您的编译器会将所有警告视为错误(-Werror)或禁用此标志。

【讨论】:

  • 如果在 int List::get() 中 current == NULL,你可以返回 -1
  • 试过了,现在只给出一个错误multiple definition of main :(
  • 不要禁用该标志。 修复警告.
  • 问题与您在回答@DídacPérez 中所说的一样,IDE 一次编译多个文件,现在我尝试使用单个文件,它工作正常。
  • 我很高兴知道您已经解决了您的问题。您能否将我的答案标记为正确答案?
【解决方案2】:

程序编译良好(yourcode.cc 包含您的源代码):

$ CXXFLAGS="-Wall -Werror -Wpedantic" make yourcode

stack.cc: In member function ‘int List::get()’:
stack.cc:76:1: error: control reaches end of non-void function [-Wreturn-type]
 }

并调用./yourcode 输出:

 List size = 5

 Element 1 2
 Element 2 6
 Element 3 8
 Element 4 7
 Element 5 1

显然,您的 IDE 将向链接器添加一些标志。请向我们展示您的编译标志/设置。查看编译日志或运行 make 命令执行更详细。

可以看看。 Code::blocks verbose build

【讨论】:

  • -1:您在这里所做的只是训练 OP 以 忽略 重要的编译器警告。为什么?我也看不出这是一个答案,更不用说 +3 答案了,因为您根本没有解决 OP 的问题。
  • 当然可以,但它表明代码本身没问题,并暗示构建环境有任何问题。
  • 但是代码不是好的。有一个警告表明这一点。您选择忽略它并教 OP 忽略它。为什么?
  • 是的,上面已经讨论过返回值的问题。您完全正确,肯定需要使用足够的标志进行编译才能显示那些简单的静态问题。
【解决方案3】:

问题只是,我的 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;
}

效果很好。

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 2023-02-01
    • 1970-01-01
    • 2015-08-25
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    相关资源
    最近更新 更多