473,394 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

creating an array of pointers

i have a "tile" class.
i want to create a 2d-array of tiles.
they should point to actual tiles to avoid storing repetitive data.

ex.
Tile *map[32][32];
map[1][6] = &tile_dirt;

This does not work. I'm assuming because it thinks I'm pointing to an
array, not creating an array of pointers. How would I fix this?

Thanks!

Sep 27 '05 #1
7 1828
I've tried this

vector< vector<Tile*> > map;

but when I try

map2[5][5] = &tile_dirt;

I get the errors:
expected constructor, destructor, or type conversion before '=' token
expected `,' or `;' before '=' token

And I dont understand why I am getting them.
Please help?

- Mark

Sep 28 '05 #2
Mark wrote:
i have a "tile" class.
i want to create a 2d-array of tiles.
they should point to actual tiles to avoid storing repetitive data.

ex.
Tile *map[32][32];
map[1][6] = &tile_dirt;
What are Tile and tile_dirt? (I assume that definitions/declarations of
these are present in the code you're compiling but you didn't post them).

This does not work.
How do you know? (i.e., tell us what "does not work" means).
I'm assuming because it thinks I'm pointing to an
array, not creating an array of pointers. How would I fix this?


It depends on what's wrong, which isn't possible to tell from the
information given. If tile_dirt is a Tile, then there's nothing wrong with
the code you posted.. If it's a Tile*, then remove the '&'. If it's
something else, then tell us what.

DW
Sep 28 '05 #3
Mark wrote:
I've tried this

vector< vector<Tile*> > map;

but when I try

map2[5][5] = &tile_dirt;
You've got 'map' above but 'map2' here.

I get the errors:
expected constructor, destructor, or type conversion before '=' token
expected `,' or `;' before '=' token

And I dont understand why I am getting them.
Did you #include <vector>?
Did you include the definition of Tile?
Did you include the definition or declaration of tile_dirt?
Do you have a "using namespace std;" or "using std::vector" that you didn't
post? (with neither, you need "std::vector")
If you do have "using namespace std;", then it is inadvisabe to use the
variable name 'map', since that is the name of a class template in the
standard library.
Please help?


I'm trying, but if there's any code you're compiling but not including in
your post, then post it. (Make the smallest program you can that exhibits
the problem, then post _all_ of it).

DW
Sep 28 '05 #4
they were both map2, i meant to change them both back to "map" in my
post, but i forgot :)

if I forgot to include vector, or the definition of Tile, it would have
given me a declaration error.

tile_dirt is a Tile (i figured that was implied -- but no, it's not a
pointer)
i am using namespace std. again, that would give me a different error.

i'll post the class, but it might be a bit confusing.

class Tile
{
public:

class Type
{
public:
float f, b;

Type( float, float );
};

class Shape
{
public:

bool n,s,w,e;
Shape( bool, bool, bool, bool );
};

Sprite *sprite;
Shape *shape;
Type *type;
unsigned short tileset,group,probability;

Tile( Sprite*, Shape*, Type*, unsigned short, unsigned short,
unsigned short );
};

"Sprite" is defined elsewhere, and the functions are also defined
outside the class.
vector< vector<Tile*> > myMap;
and
myMap[5][5] = &tile_dirt;

are global.

it's complaining about a constructor or destructor before the "=" token
in
myMap[5][5] = &tile_dirt;
which I'm not understanding...

It's a pointer, so it shouldn't need to be constructed.. right?
Plus, it's declared on the line
vector< vector<Tile*> > myMap;
So I figured it would complain about that instead.

But now I'm thinking I'm not allowed to access it like that until I do
something with it... but I'm not sure what.

Sep 28 '05 #5
Mark wrote:
they were both map2, i meant to change them both back to "map" in my
post, but i forgot :)

if I forgot to include vector, or the definition of Tile, it would
have given me a declaration error.

tile_dirt is a Tile (i figured that was implied -- but no, it's not a
pointer)
To you it might have been implied, but we don't know what you know.
i am using namespace std. again, that would give me a different
error.
Maybe, but error messages are sometimes misleading. You can't always assume
that the compiler's message makes any sense at all. In your case, there is
nothing obviously wrong with the code you posted, so the problem is likely
to be elsewhere. That's why it's best just to post what you compiled and be
done with it, and you might get your answer in one reply instead of three
(or more).
i'll post the class, but it might be a bit confusing.

class Tile
{
public:

class Type
{
public:
float f, b;

Type( float, float );
};

class Shape
{
public:

bool n,s,w,e;
Shape( bool, bool, bool, bool );
};

Sprite *sprite;
Shape *shape;
Type *type;
unsigned short tileset,group,probability;

Tile( Sprite*, Shape*, Type*, unsigned short, unsigned short,
unsigned short );
};

"Sprite" is defined elsewhere, and the functions are also defined
outside the class.
vector< vector<Tile*> > myMap;
and
myMap[5][5] = &tile_dirt;

are global.

it's complaining about a constructor or destructor before the "="
token in
myMap[5][5] = &tile_dirt;
Is that statement inside a function? What is the exact message?
which I'm not understanding...
That doesn't make sense to me, and perhaps only confirms what I said above
about error messages.
It's a pointer, so it shouldn't need to be constructed.. right?
Right.
Plus, it's declared on the line
vector< vector<Tile*> > myMap;
So I figured it would complain about that instead.
Well, it might be complaining about tile_dirt (which you said was a global
Tile, but you still didn't post the definition).
But now I'm thinking I'm not allowed to access it like that until I do
something with it... but I'm not sure what.


I get no compiler errors for the code below on either VC++ 6.0 or VS .NET
2003. Try it yourself and see if your compiler says anything, and post how
your code is different if you still can't see what's wrong.

#include <vector>

typedef int Sprite; // just to satisfy compiler

class Tile
{
public:

class Type
{
public:
float f, b;

Type( float, float );
};

class Shape
{
public:

bool n,s,w,e;
Shape( bool, bool, bool, bool );
};

Sprite *sprite;
Shape *shape;
Type *type;
unsigned short tileset,group,probability;

Tile( Sprite*, Shape*, Type*, unsigned short, unsigned short,
unsigned short );
};

using namespace std;

// initialize myMap with elements, otherwise [5][5] access
// in main() causes undefined behaviour
vector< vector<Tile*> > myMap(32, vector<Tile*>(32));
Tile tile_dirt(0, 0, 0, 0, 0, 0);

int main()
{
myMap[5][5] = &tile_dirt;
}

DW
Sep 28 '05 #6
thanks for all your help.

i have my code in multiple files and there is a lot of irrelevant
stuff, and everything depends on something else, so i'd have to post
almost my entire project...

but these are also global:
Sprite sprite_dirt("img/dirt.gif");

// tiles
Tile tile_dirt(&sprite_dirt);

your code compiles... but you also defined myMap differently.

but.. i think the problem was that i was setting
myMap[5][5] = &tile_dirt;
globally...
i suppose you can only do that in the declaration...
heh.. problem was so simple :S

a quick test:

int iii;
iii = 5;
(global)

doesn't work. produces same error. guess i forgot i couldnt do
that.... woops !

heheh.. thanks again :)

Sep 28 '05 #7
Mark wrote:
thanks for all your help.

i have my code in multiple files and there is a lot of irrelevant
stuff, and everything depends on something else, so i'd have to post
almost my entire project...
That's why I said to post the smallest program that exhibits the error. You
take stuff out, remove unused members etc., until you've got the bare bones
of the problem in a short, simple fragment of code (you can sometimes find
the bug yourself that way and not have to ask anyone).
but these are also global:
Sprite sprite_dirt("img/dirt.gif");

// tiles
Tile tile_dirt(&sprite_dirt);
This doesn't match your single Tile constructor:
Tile( Sprite*, Shape*, Type*, unsigned short, unsigned short,
unsigned short );

your code compiles... but you also defined myMap differently.
That doesn't make any difference. Try your definition and it will still
compile. It will also crash when you run it if you don't fill in the vectors
before the [5][5] access.
but.. i think the problem was that i was setting
myMap[5][5] = &tile_dirt;
globally...
i suppose you can only do that in the declaration...
No, you can only do it inside a function, like main(), as I had it, or your
own function.

heh.. problem was so simple :S

a quick test:

int iii;
iii = 5;
(global)

doesn't work. produces same error. guess i forgot i couldnt do
that.... woops !

heheh.. thanks again :)


DW
Sep 28 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Faheem Mitha | last post by:
Dear People, I have the current modest goal. Given a user defined class, I want to creata an array of instances of that class. Consider the following class. class cell: def...
9
by: sci | last post by:
I believe both ways to create an array of strings are correct. Is there any difference between these two? 1. char *MyString = {"First string", "Second string", ..."Tenth string"}; 2. char...
5
by: Chris | last post by:
Hi, to create an array of 2 objects (e.g. of type '__gc class Airplane') I need to do : Airplane * arrAirplanes __gc = new Airplane* __gc; arrAirplanes = new Airplane("N12344"); arrAirplanes...
1
by: Andrew Poulos | last post by:
If I have an array whose elements may or may not also be arrays and I’m given a string that relates to an appropriate index in that array how do I use that string as the index? For example, if I...
7
by: cdg | last post by:
Would anyone explain how to return an array that wasn`t passed from "main" using a pointer. I'm not understanding how scope and memory are involved in this. However, I do know that there are two...
1
by: yankee1423 | last post by:
Hello all, I have 3 tables with 1 field in each. The third table has half a million rows in it so I can do this with excel. What I want to do is create an array that I can export to Excel. ...
11
by: Piotrek | last post by:
Hi, I have no idea why my reallocation function (myadd) is breaking up my array. I'm trying to solve it and it took me few hours already. Maybe you can look at my code and explain me what is...
15
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array...
0
by: Mike | last post by:
I am using a COM DLL function that takes a pointer to the first element in an array of short values. Foo(short* array) COM interop provides that function to me in the .NET world with a "ref...
1
by: alansharp | last post by:
Hi guys Im attempting to write conways Game of life and need to use a pointer and 2 arrays. The reason im using a pointer is hopefully to speed up the code rather than copying the array >...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.