4

I'm new to C++, and have finally given up on trying to get this to compile after staring at it for too long. The compiler seems to be rejecting the constructor prototype in the header file for some reason... I can't figure out what's wrong with it.

Item.h:

#ifndef ITEM_H_
#define ITEM_H_


class Item {
public:
    Item(int); //This line is what Eclipse keeps flagging up with the error in the title
    virtual ~Item();
    Item* getNextPtr();
    int getValue();
    void setNextPtr(Item *);
};

#endif /* ITEM_H_ */

In my Item.cpp file I have:

int val;
Item* nextPtr = 0;
Item::Item(int value) {
    val = value;
}

Item* Item::getNextPtr() {
    return nextPtr;
}

void Item::setNextPtr(Item *nextItem) {
    nextPtr = nextItem;
} 

int Item::getValue() {
    return val;
}

Item::~Item() {
    // TODO Auto-generated destructor stub
} 

Oops, I'm using GCC. And yeah, they should have been member variables! How do I go about doing that using this format? The code where I use instantiate Item is below. I am aware that there should be no global variables in that either...

#include "LinkList.h"
#include "Item.h"

Item* first = 0;
int length = 0;

LinkList::LinkList(int values[], int size) {
    length = size;
    if (length > 0) {
        Item firstItem = new Item(values[0]);
        Item *prev = &firstItem;
        first = &firstItem;
        for (int i = 0; i < size; i++) {
            Item it = new Item(values[i]);
            prev->setNextPtr(&it);          //set 'next' pointer of previous item to current item
            prev = &it;                     // set the current item as the new previous item
        }

    }
}

LinkList::~LinkList() {
    for (int i = 0; i < length; i++) {
        Item firstItem = *first;
        Item *newFirst = firstItem.getNextPtr();
        delete(first);
        first = newFirst;
    }
}

int LinkList::pop() {
    Item firstItem = *first;
    first = firstItem.getNextPtr();
    return firstItem.getValue();
}

I've just noticed a bug with the functionality of the pop() and destructor functions... please ignore those, I just want to figure out what's wrong with the instantiation of Item.

GCC error:

Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\LinkList.o" "..\\src\\LinkList.cpp" 
..\src\LinkList.cpp: In constructor 'LinkList::LinkList(int*, int)':
..\src\LinkList.cpp:16:38: error: invalid conversion from 'Item*' to 'int' [-fpermissive]
..\src\/Item.h:14:2: error:   initializing argument 1 of 'Item::Item(int)' [-fpermissive]
..\src\LinkList.cpp:20:32: error: invalid conversion from 'Item*' to 'int' [-fpermissive]
..\src\/Item.h:14:2: error:   initializing argument 1 of 'Item::Item(int)' [-fpermissive]

21:24:26 Build Finished (took 256ms)
user1804523
  • 53
  • 1
  • 1
  • 4
  • Post the code where you **use** Item, and the complete error message from gcc (including line numbers) – gustaf r Jan 08 '13 at 21:37
  • 4
    I really think you actually want `val` and `nextPtr` to be *member variables* of `Item` rather than globals... – Andy Prowl Jan 08 '13 at 21:37
  • You are not initializing an `Item` anywhere in the posted code... – K-ballo Jan 08 '13 at 21:37
  • Eclipse is an IDE, not a compiler. It'd be useful to know what compiler you're using; along with an [SSCCE](http://sscce.org) that reproduces the problem. There's nothing syntactically wrong with your code. But why on earth are you initializing a global variable with the constructor argument? – Praetorian Jan 08 '13 at 21:38
  • g++ is reporting two errors, not four. Each error contains two locations it thinks you might want to look at. Item.h is fine, but LinkList.cpp incorrectly tries to use a declaration in Item.h. – aschepler Jan 08 '13 at 22:00

1 Answers1

4

Here:

Item firstItem = new Item(values[0]);

You are creating a new Item with an item pointer as its argument. This is the same as:

Item firstItem(new Item(values[0]));

And it should be:

Item *firstItem = new Item(values[0]);
perreal
  • 94,503
  • 21
  • 155
  • 181
  • I see, so it was more because I didn't understand how the 'new' keyword works. Thanks! How do I make variables member variables when I'm splitting the prototypes as above? – user1804523 Jan 08 '13 at 22:01
  • there are similar errors in the code and you need to fix them too. I don't understand your question above though. – perreal Jan 08 '13 at 22:03
  • Yeah, I've noticed them, thanks. I mean, how would you rewrite what I currently have in Item.cpp so that val is a member variable? – user1804523 Jan 08 '13 at 22:06
  • add a private member `val` to the `Item` class and remove the `val` from cpp. That should do it. – perreal Jan 08 '13 at 22:20