2014-10-29 3 views
1

우분투를 처음 사용하기 때문에 C++로 과제를 개발해야합니다. codeblocks IDE를 사용하여 C++ 프로그램을 작성하고 있습니다. 내가 뭔가를 컴파일 할 때마다 , 그것은 이러한 오류를 제공합니다GNU GCC 컴파일러 오류 "메인의 다중 정의"

여기
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; 
} 

사람이 설명 할 수 , 어디 을 장난합니까?

+2

'List :: get()'은 값을 반환하지 않고 (즉, if의 조건이 거짓 일 때) 함수를 종료하는 제어 경로를가집니다. – Niall

+0

컴파일러/링커 플래그를 제공해 주실 수 있습니까? – sfrehse

+0

코드를 링크로 게시하지 말고,'int List :: get()'함수를 사용하면 부울 조건이 충족되지 않으면 아무 것도 반환하지 않으므로 – EdChum

답변

0

문제는 단지 내 IDE 한 번에 여러 파일을 컴파일 하였다, 는 또한 기능 int List::get(), 에 난 후에
, if 문 제가 편집하기 전에 뜻이 함수의 끝에서 else return -1을 추가 할 수있어

: 내가 함께이 일을 대체

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

: 내 코드는 int List::get() 함수는 다음과 같았다

그리고 정상적으로 작동합니다.

3

IDE가 하나의 파일 만 컴파일하는 것이 아니라 주 기능 정의가 포함 된 IDE 인 것으로 보입니다. 얼마나 많은 파일이 컴파일되고 있는지 확인하십시오.

또한 모든 경고를 오류로 처리 (-Werror)하거나이 플래그를 비활성화합니다.

+0

방금 ​​하나의 파일을 컴파일하려고했는데 ... 같은 오류가 발생했습니다 ... – arximughal

+0

현재 int == NULL 인 경우 -1을 돌려줍니다 List :: get() –

+0

오류''다중 정의 main' :( – arximughal

3

이 프로그램은 (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

+2

-1 : 여기서 중요한 것은 컴파일러가 중요한 컴파일러 경고문을 _ignore_하도록 교육하는 것입니다. 당신이 OP의 문제를 전혀 언급하지 않았기 때문에 나는 또한 이것이 대답 인 방법을 알지 못합니다. –

+1

물론 이죠. lf는 괜찮으며 빌드 환경에 문제가 있음을 나타냅니다. – sfrehse

+0

하지만 코드는 괜찮지 않습니다. 그것을 나타내는 경고가있었습니다. 당신은 그것을 무시하고 OP가 그것을 무시하도록 가르쳤습니다. 왜? –