| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Maze problem: 2D object array within an object
Wont anyone take a look at these few classes and fix and explain why cant a person place a class(mazeItem) within another class(maze). We are trying to place a 2d array of type mazeItem within the class maze, as maze is the manager, as it were, of the whole system but it keeps on giving trouble. Now we put an array of type character and this caused no problems, so what is the problem.
Enclosed with this email are the classes in question. Thanks....i think. Oh and please email the classes back if you sort out the problem. re: clark@telkomsa.net //maze.h using namespace std; #include <string> //#include "mazeItem.h" class Maze { public: Maze(int = 20, int= 20); void addItem(string); bool decode(string); bool validate(string); bool update(); void changeCord(int, int); void fill(char); void draw(); private: int x_; //x keyboard position int y_; //y keyboard position int width_; //maze width int length_; //maze length char mazeArr[][]; //example array, to show that making an array of primitive type works MazeItem mazeItem_[][]; //array of objects, doesn't f*(&ing work }; //maze.cpp #include "maze.h" #include <iostream> #include <string> #include "mazeItem.h" using namespace std; Maze::Maze(int width, int length):width_(width-1), length_(length-1) { x_ = 0; y_ = 0; mazeArr[width_][length_]; //easy to initialize primitive type array mazeItem[width_][length_]; //cannot initialize array of objects } void Maze::addItem(string code) { Maze::decode(code); } bool Maze::decode(string code) { if (!Maze::validate(code)) { return false; } string col; string item; string row; string temp; row = code[0]; temp = code[2]; cout<<"code: "<<code<<endl; if ((temp == "0")||(temp =="1")||(temp =="2")||(temp =="3")||(temp =="4")||(temp =="5")||(temp =="6") ||(temp =="7")||(temp =="8")||(temp =="9")) { cout<<"then"<<endl; col=code.substr(1,2); item=code.substr(3, code.length()); cout<<endl<<endl; } else { col=code.substr(1,1); item=code.substr(2,code.length()); cout<<"else"<<endl; } //convert row value to int x_ = 0 ; int i = 0 ; while ( row[i] != '\0' ) { x_ = x_ * 10 ; x_ = x_ + ( row[i] - '0' ) ; i++ ; } x_ = x_ - 49; cout<<"row value converted: "<<x_<<endl; //convert column value to int y_ = 0 ; i = 0 ; while ( col[i] != '\0' ) { y_ = y_ * 10 ; y_ = y_ + ( col[i] - '0' ) ; i++ ; } cout<<"col value converted: "<<y_<<endl; cout<<"row: "<<row<<endl; cout<<"col: "<<col<<endl; cout<<"item: "<<item<<endl; return true; } bool Maze::validate(string code) { if (code.length()<=2) { cout<<"Too few characters, require at least 3"<<endl; return false; } string temp; temp = code[0]; if ((temp == "0")||(temp =="1")||(temp =="2")||(temp =="3")||(temp =="4")||(temp =="5") ||(temp =="6")||(temp =="7")||(temp =="8")||(temp =="9")) { cout<<"Start with letter coordinate first"<<endl; return false; } temp = code[1]; if (!((temp == "0")||(temp =="1")||(temp =="2")||(temp =="3")||(temp =="4")||(temp =="5") ||(temp =="6")||(temp =="7")||(temp =="8")||(temp =="9"))) { cout<<"The letter coordinate must be followed by a number"<<endl; return false; } return true; } bool Maze::update() { return true; } void Maze::changeCord(int x, int y) { x_ += x; y_ += y; } void Maze::fill(char charFill) { int a; int b; for(a = 0; a<= width_; a++) { for(b = 0; b<= length_; b++) mazeArr[a][b] = charFill; } } void Maze::draw() { int a; int c; char b; cout<<" "; for(b='A'; b<= 'A' + width_; b++) { cout<<b; } cout<<endl; for(a=0; a<=length_; a++) { if (a<10) cout<<" "; cout<<a; for(c = 0; c<=width_; c++) { cout<<mazeArr[a][c]; } cout<<endl; } } //end of maze2.cpp //mazeItem.h class MazeItem { public: MazeItem();//char = 'w'); // MazeItem(const MazeItem&); // MazeItem& operator=(&MazeItem); void setSymbol(char); char getSymbol(); protected: private: char symbol_; }; //mazeItem.cpp #include "mazeItem.h" using namespace std; MazeItem::MazeItem()//char symbol): symbol_(symbol) { } /* MazeItem::MazeItem(const MazeItem& item) { symbol_ = item.symbol_; } MazeItem& MazeItem: perator=(MazeItem& item){ if(this == &item) return *this; return *this; } */ void MazeItem::setSymbol(char symbol) { symbol_ = symbol; } char MazeItem::getSymbol() { return symbol_; } |
|
#2
|
|||
|
|||
|
Rather than posting code, swearing and saying it doesn't work try posting more specific questions and when you have errors you don't understand include the error messages with them and you're more likely to get helpful responses.
As a quick thought though, in your maze.h file you have the #include mazeItem.h line commented out so it may be that it just doesn't know what a mazeItem is at this point. -KM- |
|
#3
|
|||
|
|||
|
You haven't declared mazeItem as a class. Technically your code would read....
class Maze { public: Maze(int = 20, int= 20); .... class MazeItem { mazeItem_[];//array of objects, doesn't f*(&ing work }; }; The best suggestion, IMO, is to keep the classes seperate. It's more productive. Either that or I misinterperated the message and you've already got your code for class MazeItem...then Im just babbling on uselessly. Last edited by Frequency : June 12th, 2004 at 04:17 PM. Reason: I can't spell... |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Maze problem: 2D object array within an object |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|