Help with pointers to classes?

Hi there, I was wondering if someone could help me out with a program. It is a homeworky type problem (and the admin post said to be careful about those), but here goes. I'm trying to write a program using classes and pointers to read off parts of objects.

[a1]__________________________[a2]
[int = x][pointer to a2]______[int = y][pointer to a3]

I know my description is horrible, so I hope the figure kind of helps. I'm trying to output int x and y, but I have to use the pointer to a2 to find a2 and output y. I'm stuck because I'm not sure how to use the pointer to a2 to display parts of object a2.

I have this much worked out so far:

#include <iostream>

using namespace std;

class Numbers
{
public:
int number;
Numbers* pnumber;
};


void classifier (int *a, int n)
{
Numbers box[n];
for (int i = 0; i < n; ++i)
{
box[i].number = a[i];
box[i].pnumber = &box[i+1];
}


}
int main()
{
int store[100];
int count;
cout <<"enter numbers, 0 to stop" << endl;
for (count=0; count<100; ++count)
{
cin >> store[count];
if (store[count]==0)
{
break;
}
}
classifier(store, count);
return 0;
}

I've tried things like directly outputting everything in box[whatever] using "cout << *box[whatever]"; but that didn't work :P.

Sorry if this is a stupid question. I'd appreciate any help/criticisms.

I'm going to bed soon since it's late here in China, so I hope I don't offend anyone by not replying for like 8 hours.
Last edited on
first of all.. please use code tags!!! it makes your code much easier to read!!

and here's an error:
Numbers box[n];
you are trying to dinamicly create an array.. this is not the way to do that..

do this:
Numbers *box = new Numbers[n];

and make sure to free the space at the end of the function:
delete[] box ;

and i'm not really sure, but i don;t think this i working:
box[i].pnumber = &box[i+1];
i think you need braces around box..
box[i].pnumber = &(box[i+1]);
otherwise you get the adress of the first element instead of the one you want... (i think....)
Sorry, what's a code tag? And thanks for the advice. I'll try with the braces around box.

Edit: I tried putting braces around box[i+1] but that generates an error:
Cannot convert 'Numbers**' to 'Numbers*' in assignment
Last edited on
a code tags makes you code more readable...

example:

[code]
int main()
{
return 0;
}
[/code]

results in:

1
2
3
4
int main()
{
  return 0;
}


..and can you show me what you have now?
Last edited on
Topic archived. No new replies allowed.