I am trying to make a linked list and have followed the advice of numerous tutorials but I am having a very basic problem. Regardless, here is my bit of code:
My problem is on the last line. Regarding newCell, the compiler says "this declaration has no storage class or type specifier." I am doing it the exact way other tutorials explain it (or so I think...). I would guess that there's something simple that I'm doing wrong.
Can anybody help?
Thanks!
(And yes, it's going to be a 3-dimensional linked list, and yes, I know it's gonna be a bear to get working.)
Are you sure that's the line the error message pertains to? I couldn't see any problem here so I tried it in my own compiler. It found no problems with your code.
When I try to run this in Visual C++ 2010 Express, the log says:
1 2 3 4 5
1> Game.cpp
1>c:\users\jason\data structures\data structures game\data structures game\map.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\jason\data structures\data structures game\data structures game\map.h(14): error C2040: 'newCell' : 'int' differs in levels of indirection from 'cell *'
1>c:\users\jason\data structures\data structures game\data structures game\map.h(14): error C2440: 'initializing' : cannot convert from 'cell *' to 'int'
1> There is no context in which this conversion is possible
In Dev-C++, it says:
expected constructor, destructor, or type conversion before '=' token regarding the same line.
fun2code, which compiler are you using and do you have any ideas what could be causing this?
I'm using Visual C++ 2008 Express. The only thing I can think of is that lines 1-13 are OK to have outside of a function (you would be declaring the structure and a pointer to it as global).
Line 14 must be inside a function though. Is it? As Albatross requests, more code would be good to see so we know more about the context of your posted code.
Have you gotten a simple 1-dimensional linked list to work before? Or is this your first try at any type of linked list?
That's really all I have so far. I had to start over on this project because making a 3-dimensional array of objects was a pain in the ass, so now I'm trying to implement a linked list. All I've got is this header file and the skeleton of main.cpp.
I haven't done anything yet other than try to get this linked list started.
I found that if I put line 14 in main(), the program will compile. How would I do this if I wanted to keep it in the .h file? Would changing the struct into a class mess anything up?
EDIT:: If I were to do this, would I be on the right track?
class world{
private:
world *north;
world *south;
world *east;
world *west;
world *up;
world *down;
string location;
string description;
public:
void createWorld();
};
void world::createWorld(){
world *newCell;
newCell = new world;
}
Header files aren't really supposed to contain anything that translates directly to object code, such as variable declarations and code. I've seen and written some unorthodox exceptions, but as a general rule of thumb... no. :)
EDIT: Um... what are you doing to do with that newCell in createWorld()? If nothing, then that's a memory leak, because of the lack of a delete. :|
In that case, where would you recommend putting the code that generates the map? Would I call the function in main() or am I missing your point?
Sorry for being so slow to catch on to this, but I do appreciate your patience and the help that you're giving me.
EDIT:: In response to your edit, I am making a text-based adventure game. Each node is going to act as a location in the playable world. I'm probably doing it wrong and if I can't figure it out soon I'll call it a night and ask my professor tomorrow.
The place I'd recommend putting the code for world::createWorld() is in another C++ file, especially if you're planning something like a text-based game. Those could take up several files, easily.
I was actually curious as to what you were going to do with the newCell after everything is set up in that function, specifically how are you going to use it in the rest of your program?
To be completely honest, I'm really not sure. This is my first attempt at a linked list and my textbook is of no help.
All I am trying to do right now is make the template for the game world. I am trying to make a 7x7x3 map, but as you may have gathered by now I am kind of lost. I just need to create the nodes and link them properly at this point.
What I'd suggest instead of not returning anything is having your function return a world*... newCell, to be precise.
If you're using your nodes like that for a world map, I'd suggest adding a variable to be some sort of indication as to which way you can go each time, just to make life easier on yourself in the future. Maybe a bool travelable[6];, or something like that. ;)