dynamic memory allocation problem, please help
dynamic memory allocation problem, please help
Hi can anyone help me please, im trying to make a linked list to store info for a pathfinding algorithm, i have some structs to hold info:
// node info structure
typedef struct
{
int f, g, h; // pathfinding evaluation parameters
MapPoint parent; // parent square
int state; // indicates whether square is on open list or not
}AInode;
// data structure to create linked list
typedef struct
{
AInode node;
struct ListNode* nxt;
}ListNode;
and im trying to create a new instance of the ListNode to be the root of the list:
// Create open list
ListNode* openRoot; // create pointer to root node
ListNode* openPtr; // create pointer to move aronud list
openRoot = new AInode; // make pointer point to reserved memery
openRoot->nxt = 0; // end of chain is NULL
but when i compile I get the following error:
error: 'new' undeclared
does anyone know why this does not work? im pulling my hair out. thanks
// node info structure
typedef struct
{
int f, g, h; // pathfinding evaluation parameters
MapPoint parent; // parent square
int state; // indicates whether square is on open list or not
}AInode;
// data structure to create linked list
typedef struct
{
AInode node;
struct ListNode* nxt;
}ListNode;
and im trying to create a new instance of the ListNode to be the root of the list:
// Create open list
ListNode* openRoot; // create pointer to root node
ListNode* openPtr; // create pointer to move aronud list
openRoot = new AInode; // make pointer point to reserved memery
openRoot->nxt = 0; // end of chain is NULL
but when i compile I get the following error:
error: 'new' undeclared
does anyone know why this does not work? im pulling my hair out. thanks
i think your onto something here, im no expert with gcc+g++ more of a vsnet man. However still having trouble fixing. Changing the extension causes the compiler to not recognise the file and report that there is nothnig to be made.
Also there are several C files to be compiled in the project, i suspect I must change settings of my makefile but I dont know what to change can anyone help my current makefile looks like this:
TARGET = hello
OBJS = main.o graphics.o framebuffer.o
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
LIBDIR =
LIBS = -lpspgu -lpng -lz -lm
LDFLAGS =
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Packo Jacko 2005
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Also there are several C files to be compiled in the project, i suspect I must change settings of my makefile but I dont know what to change can anyone help my current makefile looks like this:
TARGET = hello
OBJS = main.o graphics.o framebuffer.o
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
LIBDIR =
LIBS = -lpspgu -lpng -lz -lm
LDFLAGS =
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Packo Jacko 2005
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Ok still having trouble, I have changed all files to cpp insead of c (although i havnt touched .h files) and now i ill compile mostly except for a single error:
main.cpp:(.text+0x6d0): undeclared reference to 'operator new(unsigned int)
my structs now look like this:
typedef struct MapPoint
{
int x,y;
};
typedef struct AInode
{
int f, g, h; // pathfinding evaluation parameters
MapPoint parent; // parent square
int state; // indicates whether square is on open list or not
};
// data structure to create linked list
typedef struct ListNode
{
AInode node;
ListNode* next;
};
and my calls look like this:
// Create open list
ListNode* openRoot; // create pointer to root node
ListNode* openPtr; // create pointer to move aronud list
openRoot = new ListNode; // make pinter point to reserved memery
openRoot->next = 0; // end of chain is NULL
its definately 'new' thats causing trouble, please help me im going mad over this!
main.cpp:(.text+0x6d0): undeclared reference to 'operator new(unsigned int)
my structs now look like this:
typedef struct MapPoint
{
int x,y;
};
typedef struct AInode
{
int f, g, h; // pathfinding evaluation parameters
MapPoint parent; // parent square
int state; // indicates whether square is on open list or not
};
// data structure to create linked list
typedef struct ListNode
{
AInode node;
ListNode* next;
};
and my calls look like this:
// Create open list
ListNode* openRoot; // create pointer to root node
ListNode* openPtr; // create pointer to move aronud list
openRoot = new ListNode; // make pinter point to reserved memery
openRoot->next = 0; // end of chain is NULL
its definately 'new' thats causing trouble, please help me im going mad over this!
If you're including your own C headers from C++, you'll need to wrap the includes with extern "C", e.g.
extern "C" {
#include "[c header file].h"
#include "[other c header file].h"
}
(unless that is already being done inside your header files - see the pspsdk header files for examples of headers that can be used from both C and C++)
extern "C" {
#include "[c header file].h"
#include "[other c header file].h"
}
(unless that is already being done inside your header files - see the pspsdk header files for examples of headers that can be used from both C and C++)
unfortunately that didnt work, ive tried putting everything in a single file and compling and i still get the same error. it doesnt undersand 'new'. whats going on, C++ does use this keyword doesnt it, has it been left out of the pSPSDK or something i really dont get it. Anyone please help, his is really killing me. cheers people
do you need a special package for psp and c++?
i do not have this problem, cause i did not write my objects yet
but eg:
the error msg is something like no string in std namespace
greets
lumo
i do not have this problem, cause i did not write my objects yet
but eg:
Code: Select all
std::stringstream ss;
std::string s;
s.assign("directory/filename");
ss << iNumber;
ss >> s;
ss.clear();
s.append(".extension");
greets
lumo
ok, so im trying now to use malloc to allocate the memory or the list but its still giving me trouble, this is what im doing:
struct ListNode* lroot; // create pointer to node structure
lroot = malloc(sizeof( struct Listnode)); // reserve memory the size of size ListNode
but it complains saying:
invalid conversion from void* to ListNode*
any ideas
struct ListNode* lroot; // create pointer to node structure
lroot = malloc(sizeof( struct Listnode)); // reserve memory the size of size ListNode
but it complains saying:
invalid conversion from void* to ListNode*
any ideas