gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > object containing a pointer to another object?

#173913 - zelbo - Sat May 08, 2010 12:32 am

So for my sudoku solver, right now i have a big fat "if/ else if" section that is gross and wrong and should all be replaced by pointers. Except i'm not sure how to pull it off.

I have a sudoku object that is pretty much my main game engine. This object declares and defines instances of 81 cel objects, 9 row objects, 9 column objects, and 9 block objects (cel is a instance of the Cel class, rows, columns, and blocks are all instances of the Sequence class). I would like to have the cel objects contain pointers to each sequence they belong to, so i can do something like this:
Code:
x = cel[1]->row->add(number);
y = cel[5]->column->contains(number);

where add(int number) and contains(int number) are functions of the sequence class.

Any examples of how to do this? When i google it, all i find is objects pointing to themselves and some stuff i can't make sense of.

#173914 - Dwedit - Sat May 08, 2010 2:23 am

delete
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#173915 - zelbo - Sat May 08, 2010 2:48 am

I think i've got it sorted now. Just a matter of putting the * in the right place. I wish it wasn't such a pain to post sample code, coding on a pc, internet through a mac. Weeee!

#173916 - zelbo - Sat May 08, 2010 3:17 am

Dwedit wrote:
delete


do you mean delete the post? because you're right. this is a simple thing that i was just screwing up because i have no idea what i'm doing.

#173922 - Dwedit - Sat May 08, 2010 7:15 am

I meant I made a response, then wanted to withdraw it, but this board does not have a delete button for regular users.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#173925 - zelbo - Sat May 08, 2010 7:43 pm

Ok, so currently i have something like this as a property of the cel class:

Code:
Sequence *row;


and set it like so:

Code:
cel[c]->row = &row[x];


then when i try to invoke the add() function of the row via:

Code:
cel[c]->row->add(x);


it tells me i'm trying to use Sequence** and the closest match is Sequence*
does that mean i should be doing something like this isntead?

Code:
cel[c].row->add(x);


i'll be able to get online with a friend's laptop shortly, so i'll be able to post the actual code and errors soon. These pointers are driving me crazy. i'm sure part of the problem is the fact that i'm using naked arrays, but i haven't been able to get vectors working as of yet. that will probably be my goal for the day, either getting vectors working, or figure out how to get Boost working so i can use boost arrays and smart pointers.

#173927 - vuurrobin - Sat May 08, 2010 9:35 pm

zelbo wrote:
Code:
cel[c]->row = &row[x];



is row by any change an array of Sequence pointers? or is it an array of Sequence's.


Quote:
but i haven't been able to get vectors working as of yet.


doesn't "#include <vector>" works? is the files extention .c or .cpp?
_________________
my blog:
http://vuurrobin.100webcustomers.com/

#173929 - zelbo - Sat May 08, 2010 10:36 pm

vuurrobin wrote:
zelbo wrote:
Code:
cel[c]->row = &row[x];



is row by any change an array of Sequence pointers? or is it an array of Sequence's.


cel[c]->row is a Sequence pointer, &row[x] i guess is an array of pointers (&row is anyway, [x] is the index of one Sequence in that array)

i've given up on trying to use object pointers for now (also ran into some recursive issues since i wanted them to point to each other), so for now i'm using a slightly more involved workaround where i'm giving each object an array of index ints to help them find their friends. i think it will work, but it's a bit more effort since i can't reference sequences from within cels and vice/versa. Now i'm doing more stuff from within the sudoku object itself, which will work for now.

vuurrobin wrote:

doesn't "#include <vector>" works? is the files extention .c or .cpp?


"#include <vector>" works, but i'm getting errors if i define it without declaring it and i'm not sure how to declare it properly. file extensions are .hpp or .cpp when i'm using c++ features, .c and .h if there are no c++ features that i'm aware of. i think it's that i have no grasp of the syntax for vectors.

in sudoku.hpp:
Code:
std::vector<Cel*> cel2(9);


in sudoku.cpp:
Code:
std::vector<Cel*> cel2(0) = new Cel();   


errors:
Code:
In file included from c:/devkitPro/projects/sudoku_solve/source/main.cpp:1:
c:/devkitPro/projects/sudoku_solve/include/sudoku.hpp:117: error: expected identifier before numeric constant
c:/devkitPro/projects/sudoku_solve/include/sudoku.hpp:117: error: expected ',' or '...' before numeric constant
make[1]: *** [main.o] Error 1
"make": *** [build] Error 2

> Process Exit Code: 2
> Time Taken: 00:01

#173930 - sajiimori - Sat May 08, 2010 10:59 pm

The syntax you're missing isn't about vectors specifically - it's about constructors and the 'new' operator. But actually, you can use vectors without using either of those features.
Code:
// Declaring variables.
int number;
std::vector<int> listOfNumbers;
std::vector<Cell> listOfCells;
std::vector<Cell*> listOfPointersToCells;

// Adding a value to the end of a vector, increasing its size by 1.
listOfNumbers.push_back(123);

// Instantly setting the size of a vector.
listOfNumbers.resize(20);