• HomeBoards
  • RulesRules
  • HelpHelp
  • WikiWiki
  • Donate

Author Topic: C++ pointer help?  (Read 3011 times)

mercluke (OP)

  • Posts: 6
C++ pointer help?
« on: September 19, 2010, 11:41:35 pm »
so i'm a bit new to dingoo (still waiting on my unit to arrive, should be here today or tomorrow) and in anticipation i thought i'd have a look at development for the dingoo...

so until the dingoo actually arrives i'm stuck with just writing some basic stuff and hoping it compiles and i've made a simple linked list to try and refresh my self about pointers..

makefile:
Code: [Select]
TOOLCHAINDIR := /dingux
BINPATH := $(TOOLCHAINDIR)/bin

ARCH := mipsel-linux-uclibc-
CC := ${BINPATH}/$(ARCH)g++

PROGRAM = HelloWorld.cpp
TARGET := HelloWorld.dge

all:
$(CC) $(PROGRAM) -o $(TARGET)

HelloWorld.h
Code: [Select]
#include <stdio.h>
#include "linkList.h"

void addToList(void);
linkList* pHead;

HelloWorld.cpp
Code: [Select]
#include "HelloWorld.h"

int main()
{
for(int i = 0; i < 10;i++)
{
addToList();
}
linkList* pTemp;
pTemp = pHead;
while(pTemp!=NULL)
{
pTemp->printInt();
pTemp = pTemp->pNext;
}
return 0;
}

void addToList(void)
{
linkList* pTemp;
pTemp = new linkList;
pTemp->pNext = pHead;
pHead = pTemp;
}

linkList.h
Code: [Select]
#include <stdio.h>

class linkList
{
public:
linkList(void);
~linkList(void);
linkList* pNext;
void printInt(void);
private:
int num;
};

linkList.cpp
Code: [Select]
#include <linkList.h>

linkList::linkList(void)
{
pNext = NULL;
num = 3;
}

linkList::~linkList(void)
{

}

void linkList::printInt(void)
{
printf("%i ", num);
}

but when typing make i get
Quote
# make
/dingux/bin/mipsel-linux-uclibc-g++ HelloWorld.cpp -o HelloWorld.dge
/tmp/ccnngv4m.o: In function `addToList()': HelloWorld.cpp:(.text+0x44): undefined reference to `linkList::linkList()'
/tmp/ccnngv4m.o: In function `main': HelloWorld.cpp:(.text+0x164): undefined reference to `linkList::printInt()'
collect2: ld returned 1 exit status
make: *** [all] Error 1

would anyone be able to point a noob in the right direction?

edit: forgot to mention that i'm using SiENcE's prebuilt Dingux Toolchain for Windows
« Last Edit: September 20, 2010, 08:06:36 am by mercluke »

xdpirate

  • * Former Staff
  • Posts: 490
Re: C++ pointer help?
« Reply #1 on: September 20, 2010, 02:50:41 am »
The toolchain (well, GCC really) often throws a hissy fit for stupid, little easy-to-miss mistakes. Try declaring your main function with parameters. Instead of just "int main()", do "int main(int argc, char *argv[])". I've had such errors that got fixed by declaring with parameters, hope this works for you as well!

EDIT: Just noticed: You're including stdio.h? I'm more familiar with C than C++, but I think C libraries are named differently in C++, so to import <stdio.h>, you'd import <cstdio> instead. I'm also pretty sure you're using functions from stdlib in there ("cstdlib" in C++).

EDIT 2: Disclaimer: I may be way off, it's 4 AM here and I'm tired as hell.
« Last Edit: September 20, 2010, 02:56:28 am by xdpirate »

mercluke (OP)

  • Posts: 6
Re: C++ pointer help?
« Reply #2 on: September 20, 2010, 04:52:15 am »
The toolchain (well, GCC really) often throws a hissy fit for stupid, little easy-to-miss mistakes. Try declaring your main function with parameters. Instead of just "int main()", do "int main(int argc, char *argv[])". I've had such errors that got fixed by declaring with parameters, hope this works for you as well!

EDIT: Just noticed: You're including stdio.h? I'm more familiar with C than C++, but I think C libraries are named differently in C++, so to import <stdio.h>, you'd import <cstdio> instead. I'm also pretty sure you're using functions from stdlib in there ("cstdlib" in C++).

EDIT 2: Disclaimer: I may be way off, it's 4 AM here and I'm tired as hell.

well, my orig hello world which was just

#include <stdio.h>
int main()
{
prinft("Hello world");
return 0;
}

or something to that effect seemed to compile fine but when i get home i'll give your sugggestions a try :)
i think it may be the "int main(int argc, char *argv[])" thing you mentioned with gcc being picky as with a few simple changes (including that crappy stdafx.h header that VS seems to require) it compiles fine for pc in visual studio...

edit: nope, tried "int main(int argc, char *argv[])",
tried including cstdio instead of stdio.h,
tried including cstdlib as well

doesn't work :(
« Last Edit: September 20, 2010, 08:09:35 am by mercluke »

darfgarf

  • Guest
Re: C++ pointer help?
« Reply #3 on: September 20, 2010, 09:39:27 am »
toolchaindir, might need to be /usr/bin (but that depends on toolchain)

if its c++ you shouldnt be using printf :p

#include <iostream>
using namespace std;

int main(void) //perfectly acceptible for g++
{
cout << "Hellollo, World!\n";
return 0;
}

try that :)

mercluke (OP)

  • Posts: 6
Re: C++ pointer help?
« Reply #4 on: September 20, 2010, 12:00:24 pm »
tried changing linklist.cpp and linklist.h to reflect suggestions but still not working :(

(also, @darfgarf: if the directory was /usr/bin instead of /bin then i wouldn't even be getting errors, i'd be told "command not found" :P)


xdpirate

  • * Former Staff
  • Posts: 490
Re: C++ pointer help?
« Reply #5 on: September 28, 2010, 03:45:09 am »
Hm, maybe this is a scope issue? Try shoving all the code in the main file and see if it works.

darfgarf

  • Guest
Re: C++ pointer help?
« Reply #6 on: September 28, 2010, 08:53:04 am »
or do something like

linklist list;

list.linklist* listpointer;

though from the error it looks like you're just not including the header that declares the linklist class in main, who knows, it's c++ XD

pcercuei

  • Posts: 1709
    • My devblog
Re: C++ pointer help?
« Reply #7 on: September 28, 2010, 08:18:06 pm »
Quote
# make
/dingux/bin/mipsel-linux-uclibc-g++ HelloWorld.cpp -o HelloWorld.dge
/tmp/ccnngv4m.o: In function `addToList()': HelloWorld.cpp:(.text+0x44): undefined reference to `linkList::linkList()'
/tmp/ccnngv4m.o: In function `main': HelloWorld.cpp:(.text+0x164): undefined reference to `linkList::printInt()'
collect2: ld returned 1 exit status
make: *** [all] Error 1

The problem lies in your makefile (which is weird, by the way).
On your "PROGRAM" variable, you should set all the .cpp files, including your linkList.cpp. Here, it tries to create a binary from only a part of your program, that's why it fails ;)

 

Post a new topic