1

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?

mlwn
  • 1,156
  • 1
  • 10
  • 25
arximughal
  • 270
  • 3
  • 16

3 Answers3

3

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.

Didac Perez Parera
  • 3,734
  • 3
  • 52
  • 87
3

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

Community
  • 1
  • 1
sfrehse
  • 1,062
  • 9
  • 22
  • 2
    -1: All you're doing here is training the OP to _ignore_ important compiler warnings. Why? I also don't see how this is an answer, let alone a +3 answer, since you didn't address the OP's problem at all. – Lightness Races in Orbit Oct 29 '14 at 12:54
  • 1
    Sure, but it shows that the code itself is okay and implies anything is wrong with the build environment. – sfrehse Oct 29 '14 at 13:37
  • But the code is _not_ okay. There was a warning indicating that. You chose to ignore it and taught the OP to ignore it. Why? – Lightness Races in Orbit Oct 29 '14 at 14:36
  • Yes, the problem with the return value was already discussed above. You are completely right, compilation with sufficient flags is definitely required to show those simple static problems. – sfrehse Oct 29 '14 at 14:42
0

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.

msrd0
  • 7,816
  • 9
  • 47
  • 82
arximughal
  • 270
  • 3
  • 16