Need help with my coursework :(
Hi everyone !
Im trying to write a simple data structure in c++ which works somewat like queue. the only diff is it can insert elements into various positions . I tried to use vector but it doesnt work since it can only insert const element :(. Here is the outline of the Queue class:
MakeQueue(Elements) : create a queue with the given elements and return it
Empty(Queue) return true if the queue is empty
RemoveFront(Queue): Removes the element @the head of the queue and return it
Insert(Elements, Queue): insert a set of elements into the queue, and return the new queue
Thx for reading my post
PS: just so no1 thinks im trying to get other to do my work for me. This is just a small part of my coursework, unfortunatelly :(
EDIT: remove teh urgent bit
[Edited by - Tutukun on November 16, 2005 10:10:50 AM]
Quote:
I tried to use vector but it doesnt work since it can only insert const element :(.
Do you mean STL vector template? I don't use STL often, so chances are I'm wrong, but I don't think they accept only const objects...
that wat i thought as well. But this is the error msg i got :
error C2440: 'initializing' : cannot convert from 'const int [3][3]' to 'int [3][3]'
There is no context in which this conversion is possible
here is the line that generate that error:
ExpandedStates.push_back(State);
Here r declarations of ExpandedStates and State:
vector<int[3][3]> ExpandedStates;
int State[3][3];
error C2440: 'initializing' : cannot convert from 'const int [3][3]' to 'int [3][3]'
There is no context in which this conversion is possible
here is the line that generate that error:
ExpandedStates.push_back(State);
Here r declarations of ExpandedStates and State:
vector<int[3][3]> ExpandedStates;
int State[3][3];
try storing just the reference to the array...that should work
like:
vector<*int> expandedStates;
int *state = new int[3][3];
expandedStats.push_back(state);
just form the top of my head...hope it works ;)
like:
vector<*int> expandedStates;
int *state = new int[3][3];
expandedStats.push_back(state);
just form the top of my head...hope it works ;)
Quote:Original post by Tutukun
that wat i thought as well. But this is the error msg i got :
error C2440: 'initializing' : cannot convert from 'const int [3][3]' to 'int [3][3]'
There is no context in which this conversion is possible
here is the line that generate that error:
ExpandedStates.push_back(State);
Here r declarations of ExpandedStates and State:
vector<int[3][3]> ExpandedStates;
int State[3][3];
You can insert any type of object in a vector. They are not required to be const.
I would suggest encapsulating the 3x3 array into a class. That way you can avoid using pointers alltogether:
// obviously this class should be named after whatever it represents as// IMO State is a terrible name since it could mean anything.// for example if it's a tic-tac-toe board state, call it GameBoardState or// something along that line. class State {protected: int my_array[3][3]public: State() { // initialize the my_array with proper values here } // add functions for accessing my_array here // or if you're just going to use get/set methods anyway, make it // public}// vector code:std::vector<State> my_states;State some_state();my_states.push_back(some_state);
Tom
Tutukun
I may be wrong, but I believe the error about const int and int you were receiving was because of the fact that you can't copy an array to another array (including during initialization). Plus it's not clear in your code whether or not your int State[3][3] has been initialized itself or if it's just holding garbage values.
But once again, I could be totally wrong here, and besides, the previous suggestions are probably the better route to go anyways.
I may be wrong, but I believe the error about const int and int you were receiving was because of the fact that you can't copy an array to another array (including during initialization). Plus it's not clear in your code whether or not your int State[3][3] has been initialized itself or if it's just holding garbage values.
But once again, I could be totally wrong here, and besides, the previous suggestions are probably the better route to go anyways.
yup passing the pointer to a class cointain the State work fine. Thx guys.
EDIT: problem solved, thx for reading :D
ps: thx cignox1 and c0uchm0nster
[Edited by - Tutukun on November 16, 2005 11:56:40 AM]
EDIT: problem solved, thx for reading :D
ps: thx cignox1 and c0uchm0nster
[Edited by - Tutukun on November 16, 2005 11:56:40 AM]
Quote:
the problem is probly because the GetState() doesnt match with the one in the header, but i couldnt see why?
ps: can any1 tells me how to post source code into a window just like dimelbolt did
use [ source] and [ /source] tags without spaces
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement