Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

Pointer and array problem

This topic is 4897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive got a class which is supposed to hold a number of integers. When I create an instance of the class I want to send in an array of integers that is to be copied into the class's internal array. However I get the error message:
22 D:\Documents\CPP\AI\main.cpp expected primary-expression before ';' token 
And I dont quite know what it means or what I should do. Is this the right way Im doing it? I give the constructor a int pointer as argument and the function store the numbers in the classarray until it have stored the same amount of numbers as it is in the cell size. Is there a better way? Sorry if you dont understand, ask and I'll try to give more info.
#include <iostream>

using namespace std;

const int CELL_SIZE = 40;

int main(int argc, char *argv[])
{
    return 1;
}

class bot {
	bot(int* incoming);	// constructor
	int cell[CELL_SIZE];
};

bot::bot(int* incoming) {
	for(int i = 0; i < CELL_SIZE; i++) {
		cell = incoming*;
		incoming++;
	}
}


Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Mizipzor
Ive got a class which is supposed to hold a number of integers. When I create an instance of the class I want to send in an array of integers that is to be copied into the class's internal array.

However I get the error message:
22 D:\Documents\CPP\AI\main.cpp expected primary-expression before ';' token 

And I dont quite know what it means or what I should do.

Is this the right way Im doing it? I give the constructor a int pointer as argument and the function store the numbers in the classarray until it have stored the same amount of numbers as it is in the cell size. Is there a better way?

Sorry if you dont understand, ask and I'll try to give more info.

*** Source Snippet Removed ***


Problem is in these lines:

Quote:

cell = incoming*;
incoming++;


replace them with:

Quote:

cell = incoming;


Though if it were me I would use std::vectors instead and pass references to them...

Share this post


Link to post
Share on other sites
Or, only replace:

cell = incoming*;

with

cell = *incoming;

To dereference a pointer, the * should go before the variable name, not behind it. View it like this:

int *incoming --> int (*incoming)

So *incoming is an int.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just try

cell = *incoming;

instead

cell = incoming*;

Share this post


Link to post
Share on other sites
ScootA: I thought about using vectors but since Im not going to resize them or anything, this is just a simple little program, I decided it would be easier to go with old arrays. :P But can I send a vector adress and just do a = b? That would be nice. :P

rick_appleton, Anonymous Poster: It worked that way... for some reason I thought it didnt matter at which end of the name the star was put. :P Thanks anyway!

Share this post


Link to post
Share on other sites
Quote:
Original post by Mizipzor
ScootA: I thought about using vectors but since Im not going to resize them or anything, this is just a simple little program, I decided it would be easier to go with old arrays. :P But can I send a vector adress and just do a = b? That would be nice. :P

rick_appleton, Anonymous Poster: It worked that way... for some reason I thought it didnt matter at which end of the name the star was put. :P Thanks anyway!


I wouldn't send pointers to vectors - that's just messy - using vectors your code can support arbitrary array sizes:


#include <iostream>

// this next line is the devil's code
//using namespace std;

int main(int argc, char *argv[])
{
return 1;
}

class bot
{
public:
//pass a reference to an std::vector...
bot(std::vector<int> &nData); // constructor

protected:
//here's your data
std::vector<int> _data;
};

bot::bot(std::vector<int> &nData)
{
for(int i = 0; i < nData.size; i++)
_data.push_back(nData);
}




Piece'o'cake

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!