우분투를 처음 사용하기 때문에 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;
}
사람이 설명 할 수 , 어디 을 장난합니까?
'List :: get()'은 값을 반환하지 않고 (즉, if의 조건이 거짓 일 때) 함수를 종료하는 제어 경로를가집니다. – Niall
컴파일러/링커 플래그를 제공해 주실 수 있습니까? – sfrehse
코드를 링크로 게시하지 말고,'int List :: get()'함수를 사용하면 부울 조건이 충족되지 않으면 아무 것도 반환하지 않으므로 – EdChum