Reputation: 270
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?
Upvotes: 1
Views: 2223
Reputation: 270
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();
}
I replaces this one with:
int List::get() {
if (currentNode != NULL)
return currentNode->get();
else return -1;
}
and it worked fine.
Upvotes: 0
Reputation: 1072
The program compiles fine with (yourcode.cc contains your sourcecode):
$ 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]
}
and invoking ./yourcode
outputs:
List size = 5
Element 1 2
Element 2 6
Element 3 8
Element 4 7
Element 5 1
Obviously your IDE is going to add some flags to the linker. Please show us your compile flags/setting. See the compile log or run the make command executing more verbose.
May have a look at. Code::blocks verbose build
Upvotes: 3
Reputation: 3824
It seems that your IDE is not just compiling one single file, but another one which also contains a definition of the main function. Please check out how many files are being compiled.
In addition, your compiled is treating all the warnings as errors (-Werror) or disable this flag.
Upvotes: 3