Public Group

Pointer and array problem

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

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 on other sites
Quote:
 Original post by MizipzorIve 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 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 on other sites
Just try

cell = *incoming;

cell = incoming*;

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 on other sites
Quote:
 Original post by MizipzorScootA: 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. :Prick_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);	// constructorprotected:   //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

• 22
• 10
• 19
• 14
• 14