i have 2 following files.
I compiled it with g++ in cygwin.
i get errors.
here is my 2 files. -
/* array2d.h */
-
#ifndef _ARRAY2D_H_
-
#define _ARRAY2D_H_
-
#include "exception.h"
-
-
namespace Game {
-
-
/*** Simple template class for a 2D Array ***/
-
template<class T>
-
class Array2D {
-
public:
-
Array2D(unsigned width, unsigned height) : xsize(width), ysize(height) {
-
array = new T[xsize * ysize];
-
}
-
~Array2D() { delete array; }
-
void Put(unsigned x, unsigned y, T item) {
-
if (x >= xsize || y >= ysize) {
-
throw GameException("Array out of bounds");
-
}
-
array[y * xsize + x] = item;
-
}
-
T Get(unsigned x, unsigned y) const {
-
if (x >= xsize || y >= ysize) {
-
throw GameException("Array out of bounds");
-
}
-
return array[y * xsize + x];
-
}
-
void SetAll(T item) {
-
for (unsigned i = 0; i < xsize * ysize; i++) {
-
array[i] = item;
-
}
-
}
-
private:
-
const unsigned xsize;
-
const unsigned ysize;
-
T* array;
-
};
-
}
-
#endif
-
-
/* screen.h */
-
#ifndef _SCREEN_H_
-
#define _SCREEN_H_
-
#include <string>
-
#include <iostream>
-
#include <fstream>
-
#include "array2d.h"
-
-
namespace Game{
-
using namespace std;
-
-
class Screen{
-
public:
-
Screen(){
-
Xsize = 80;
-
Ysize = 23;
-
Array2D <char> myArray(Xsize,Ysize);
-
};
-
-
~Screen(){}
-
-
/*
-
Insert a character into the screen image at a specified row and column.
-
*/
-
void Insert(){
-
char c = 'H';
-
myArray.Put(68, 23,c);
-
}
-
-
void setStatus(string str){
-
status = str;
-
}
-
-
/* clear the previous screen and display new screen image with Xsize Ysize */
-
void Paint(){
-
/* clear the screen image */
-
system("clear");
-
cout << myArray.Get(Xsize , Ysize);
-
}
-
-
/* display the help.txt and allow */
-
int displayHelp(){
-
char c;
-
char text;
-
ifstream in("help.txt");
-
-
system("stty raw -echo");
-
cin.get(c);
-
system("stty raw -echo");
-
-
if(c == 'h' || c == 'H')
-
{
-
if(!in){
-
cout << "Unable to open help.txt file" << endl;
-
return 1;
-
}
-
-
while(in >> text){
-
myArray.Put(Xsize, Ysize, text);
-
}
-
}
-
}
-
private:
-
unsigned Xsize;
-
unsigned Ysize;
-
string status;
-
};
-
}
-
#endif
-
I compiled using above exactly same code together, i get this error:
i thought it was the Array2D initialization issue, so i initialise it under "private" section as following:
Still, i get another error :
Please tell me where should change the code about the Array2D ...?
thanks in advance.
Nicky Eng
6 5846
Note: I cannot change the code in array2d.h header file, what can be change is only within screen.h header file.
thanks.
DeMan 1,806
Top Contributor
becauss myArray is declared inside a method, it's scope is limited to that method. When the compiler comes accross the variable in another method, as far as it is concerned, this is another (different) variable.
Possible solutions to this woudl be to pass it through the methods, or to declare it outside the methods so that it is visible to the entire class.
becauss myArray is declared inside a method, it's scope is limited to that method. When the compiler comes accross the variable in another method, as far as it is concerned, this is another (different) variable.
Possible solutions to this woudl be to pass it through the methods, or to declare it outside the methods so that it is visible to the entire class.
I tried it already.
I put
under " public:" , and then i put "myArray(Xsize,Ysize);" inside constructor.
it give me the 2nd error (post number 1 printscreen error image 2).
horace1 1,510
Recognized Expert Top Contributor
I tried it already.
I put
under "public:" , and then i put "myArray(Xsize,Ysize);" inside constructor.
it give me the 2nd error (post number 1 printscreen error image 2).
think in the constructor you need to have Xsize, Ysize and myArray in an initialisation list, e.g. -
Screen(): Xsize(80) , Ysize (23), myArray (Xsize,Ysize){};
-
~Screen(){}
-
and put myArray under private: -
private:
-
unsigned Xsize;
-
unsigned Ysize;
-
Array2D <char> myArray;
-
string status;
-
DeMan 1,806
Top Contributor
<removed>
Sorry, as always I was beaten whilst I typed
thank you Horace1.
it helped.
thanks again.
:D -
Screen(): Xsize(80) , Ysize (23), myArray (Xsize,Ysize){};
-
This is made me confused.
I have to get learn of it.
NickyEng.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mike Schinkel |
last post by:
I'm trying to set up a Windows 2003 server using ASP, and I am find ASP
pages just STOP as if I had called Response.End whenever an undeclared
variable is found. If I turn off Option Explicit, it...
|
by: craigbeanhead |
last post by:
Hi,
I'm teaching myself C from K&R2. I've come across something that I
really don't understand. When I try to compile the following code (in
VC++7), I get an "undeclared identifier" error. When...
|
by: W. Van Hooste |
last post by:
Just starting with C, can somebody explain why this does not work or
point me in the right direction? I wrote some tools and did some
coding but cant seem to get this one. I DID declare my FILE...
|
by: ~~~ .NET Ed ~~~ |
last post by:
It is not the first time I see this happen. I am using VS.NET 2003 with .NET
Framework 1.1. In this particular situation I have a custom user control in
a windows form. There is a member variable...
|
by: Michael Howes |
last post by:
I have a strange "warning" that shows up during design time in my current
project.
I have a UserControl, which contains a public enum called ViewTypes
This UserControl also has a public property...
| |
by: hui |
last post by:
Here is a problem I am having with web form designer. I have a database
control in the form, and setup the connection string as a dynamic
property. It compiles and runs fine.
I close the aspx...
|
by: VB Programmer |
last post by:
I wanted to access some properties of a panel control that is on a different
form. So, I went to the (Declarations) section of my main form (the form
with the panel) and changed the panel (called...
|
by: mros |
last post by:
I've created three buttons on a form (IsMdiContainer=True), it's mdi parent.
I need to disable these buttons from one of my child form. So,
I've changed these buttons modifier to Friend Shared...
|
by: nsikkandar |
last post by:
I created a Child Form and added several controls. One of the control
is GroupBox1. When I want to change the access modifier property of the
GroupBox1
FROM
"Public WithEvents GroupBox3 As...
|
by: Alfred |
last post by:
On Ubuntu, I've been experimenting with Kazuho Oku's fascinating Linux-
based perl/C trick to implement C and C++ as a scripting language...
...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |