C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 12th, 2004, 12:10 PM
COS214 COS214 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: In the south of Africa
Posts: 5 COS214 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation Maze problem: how do you place a 2D object array within an class

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. oh, and we are using linux
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_;
}
Attached Files
File Type: zip cos214.zip (2.0 KB, 769 views)

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Maze problem: how do you place a 2D object array within an class


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek