I am new to C ++ and finally gave up trying to get it to compile by looking at it for too long. Apparently, the compiler rejects the prototype constructor in the header file ... I can't figure out what's wrong with it.
Item.h:
#ifndef ITEM_H_ #define ITEM_H_ class Item { public: Item(int);
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() {
Sorry, I am using GCC. And yes, they were supposed to be member variables! How can I do this using this format? The code in which I use the Item instance is given below. I know that global variables should not be ...
#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 = ⁢ // 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 just noticed an error with the functionality of the pop () functions and the destructor ... Please ignore them, I just want to find out what is wrong with creating the element.
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)
source share