Hello All,
New to the forums, but glad I found them, looks like my kind of place :)
I recently started learning C++ here at university (in 1st year of robotics) and am loving it. Finally a way to code my ideas into something tangible!
We are upto Arrays and I am using them in part to create a 2d sim city strategy game. It is a simple project that I am using to expand my knowledge and skills as I learn the c++ language.
I am sure there are easier ways to approach this, but alas, I am not that advanced yet. We have just covered the basics and now are moving into some more advanced subjects (such as classes/arrays/pointers soon).
So far, in my game, I have been using 2D arrays to store data in relation to a grid in the game. Ie. ArrayTile [5][9] shows the tile type in the 9th x position and 5th y position on a 1st quadrent coordinate grid. Further 3D arrays are used to define multiple data in a single x-y position.
I don't really like arrays in this instance because they require a lot of extra coding and if one thing changes, a lot of the way the code is written must be changed.
So I designed an class that would hold all the data of each tile, and I was hoping to define an array by this class. Therefore, each array 'cell' would hold the data for that cell based on the class object.
However, when trying to initialize the array, I get the an error telling me basically that it is not possible.
Ie. Class is "Unit()" that has a blank constructor (so you can use Unit() to define an variable), Array is called "TestArray[20][20]" (2D 20x20 array).
I have "Unit TestArray[20][20];" which doesn't work. (I was thinking along the lines of "int TestArray[20][20];"
So, is there any simple, not totally complex way to achieve what I am looking for?
Basically, the Array is going to be called upon based on an x and y coordinate. The array, being part of the defined class, would then have a bunch of variables already stored in a single cell.
Thank you,
SexyElf
C++ Beginner : Defining an Array by class
Started by SexyElf, Mar 13 2007 03:58 PM
7 replies to this topic
#1
Posted 13 March 2007 - 03:58 PM
#2
Posted 13 March 2007 - 04:23 PM
A good advice would be to pay a visit to your universities library. I'm sure they have at least one book on c/c++. If you plan on getting into c++ for real buying one could be a good idea. O'reilly books are always a good place to start and should be avaible at any bigger bookstore or Amazon
If you want help from anybody here the least you have to do is post the error you are getting because otherwise noone will be able to understand your problem
If you want help from anybody here the least you have to do is post the error you are getting because otherwise noone will be able to understand your problem
If Prolog is the answer, what is the question ?
#3
Posted 13 March 2007 - 04:33 PM
The syntax should work so you're doing something wrong.
void test()
{
struct Unit
{
Unit() : dummy(123) {}
int dummy;
};
Unit TestArray[20][20];
}
Compiles fine. Please post your error.
#4
Posted 13 March 2007 - 05:28 PM
Hmmm, interesting, well here is what I have. I am wondering if I have to define that an array is being used as the variable somewhere in the class?
Thanks for the help.
Tileunit Array[17][20];
Error 1 error C2146: syntax error : missing ';' before identifier 'Array' d:\my documents\visual studio 2005\projects\citysim\citysim\main.cpp 32
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int d:\my documents\visual studio 2005\projects\citysim\citysim\main.cpp 32
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int d:\my documents\visual studio 2005\projects\citysim\citysim\main.cpp 32
The Class:
class Tileunit
{
public:
Tileunit();
private:
//misc variables
};
Tileunit::Tileunit()
{
name = "Field";
type = 1;
minfood = 0;
maxfood = 1;
minprod = 0;
maxprod = 1;
mingold = 0;
maxgold = 1;
upkeepfood = 0;
upkeepprod = 0;
upkeepgold = 0;
forestgrow = 0;
worker = 0;
}
Thanks for the help.
Tileunit Array[17][20];
Error 1 error C2146: syntax error : missing ';' before identifier 'Array' d:\my documents\visual studio 2005\projects\citysim\citysim\main.cpp 32
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int d:\my documents\visual studio 2005\projects\citysim\citysim\main.cpp 32
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int d:\my documents\visual studio 2005\projects\citysim\citysim\main.cpp 32
The Class:
class Tileunit
{
public:
Tileunit();
private:
//misc variables
};
Tileunit::Tileunit()
{
name = "Field";
type = 1;
minfood = 0;
maxfood = 1;
minprod = 0;
maxprod = 1;
mingold = 0;
maxgold = 1;
upkeepfood = 0;
upkeepprod = 0;
upkeepgold = 0;
forestgrow = 0;
worker = 0;
}
#5
Posted 13 March 2007 - 05:48 PM
Have you made sure that the class is visible at the point of the declaration of 'Array'?
p.s. Please use the code-tags when posting code, thank you.
#include "TheFileWhichContainsTileunit.h" Tileunit Array[17][20];
p.s. Please use the code-tags when posting code, thank you.
"Stupid bug! You go squish now!!" - Homer Simpson
#6
Posted 13 March 2007 - 06:24 PM
Thanks for all your help.
I solved the error, seems so small... sigh. I was using #ifndef class_name to define the class, and this had a spelling error, thus the class was never defined :(
I solved the error, seems so small... sigh. I was using #ifndef class_name to define the class, and this had a spelling error, thus the class was never defined :(
#7
Posted 13 March 2007 - 06:31 PM
BTW, you shouldn't use the actual class name for that #ifndef stuff. Use a token that you won't use anywhere else, like MYCLASS_H if your class is MyClass.
reedbeta.com - developer blog, OpenGL demos, and other projects
#8
Posted 19 March 2007 - 04:01 AM
Reedbeta said:
BTW, you shouldn't use the actual class name for that #ifndef stuff. Use a token that you won't use anywhere else, like MYCLASS_H if your class is MyClass.
Yes, I was just using that was an example :P
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











