473,473 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

undeclared variable...

254 Contributor
i have 2 following files.
I compiled it with g++ in cygwin.
i get errors.

here is my 2 files.
Expand|Select|Wrap|Line Numbers
  1. /* array2d.h  */
  2. #ifndef _ARRAY2D_H_
  3. #define _ARRAY2D_H_
  4. #include "exception.h"
  5.  
  6. namespace Game {
  7.  
  8.     /*** Simple template class for a 2D Array ***/
  9.     template<class T>
  10.     class Array2D {
  11.     public:
  12.         Array2D(unsigned width, unsigned height) : xsize(width), ysize(height) {
  13.             array = new T[xsize * ysize];
  14.         }
  15.         ~Array2D() { delete array; }
  16.         void Put(unsigned x, unsigned y, T item) {
  17.             if (x >= xsize || y >= ysize) {
  18.                 throw GameException("Array out of bounds");
  19.             }
  20.             array[y * xsize + x] = item;
  21.         }
  22.         T Get(unsigned x, unsigned y) const {
  23.             if (x >= xsize || y >= ysize) {
  24.                 throw GameException("Array out of bounds");
  25.             }
  26.             return array[y * xsize + x];
  27.         }
  28.         void SetAll(T item) {
  29.             for (unsigned i = 0; i < xsize * ysize; i++) {
  30.                 array[i] = item;
  31.             }
  32.         }
  33.     private:
  34.         const unsigned xsize;
  35.         const unsigned ysize;
  36.         T* array;
  37.     };
  38. }
  39. #endif
  40.  
Expand|Select|Wrap|Line Numbers
  1. /* screen.h */
  2. #ifndef _SCREEN_H_
  3. #define _SCREEN_H_
  4. #include <string>
  5. #include <iostream>
  6. #include <fstream>
  7. #include "array2d.h"
  8.  
  9. namespace Game{
  10.     using namespace std;
  11.  
  12.     class Screen{
  13.         public:
  14.         Screen(){
  15.             Xsize = 80;
  16.             Ysize = 23;
  17.             Array2D <char> myArray(Xsize,Ysize);
  18.         };
  19.  
  20.         ~Screen(){}
  21.  
  22.         /*
  23.         Insert a character into the screen image at a specified row and column.
  24.         */
  25.         void Insert(){
  26.             char c = 'H';
  27.             myArray.Put(68, 23,c);
  28.         }
  29.  
  30.         void setStatus(string str){
  31.             status = str;
  32.         }
  33.  
  34.         /* clear the previous screen and display new screen image with Xsize Ysize */
  35.         void Paint(){
  36.             /* clear the screen image */
  37.             system("clear");
  38.             cout << myArray.Get(Xsize , Ysize);
  39.         }
  40.  
  41.         /* display the help.txt and allow   */
  42.         int displayHelp(){
  43.             char c;
  44.             char text;
  45.             ifstream in("help.txt");
  46.  
  47.             system("stty raw -echo");
  48.             cin.get(c);
  49.             system("stty raw -echo");
  50.  
  51.             if(c == 'h' || c == 'H')
  52.             {
  53.                 if(!in){
  54.                     cout << "Unable to open help.txt file" << endl;
  55.                     return 1;
  56.                 }
  57.  
  58.                 while(in >> text){
  59.                     myArray.Put(Xsize, Ysize, text);
  60.                 }
  61.             }
  62.         }
  63.         private:
  64.         unsigned Xsize;
  65.         unsigned Ysize;
  66.         string status;
  67.     };
  68. }
  69. #endif
  70.  
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:
Expand|Select|Wrap|Line Numbers
  1. Array2D <char> myArray;
  2.  
Still, i get another error :



Please tell me where should change the code about the Array2D ...?
thanks in advance.
Nicky Eng
Dec 11 '06 #1
6 5846
nickyeng
254 Contributor
Note: I cannot change the code in array2d.h header file, what can be change is only within screen.h header file.

thanks.
Dec 11 '06 #2
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.
Dec 11 '06 #3
nickyeng
254 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.

I tried it already.
I put
Expand|Select|Wrap|Line Numbers
  1. Array2D<char> myArray;
  2.  
under "public:" , and then i put "myArray(Xsize,Ysize);" inside constructor.

it give me the 2nd error (post number 1 printscreen error image 2).
Dec 11 '06 #4
horace1
1,510 Recognized Expert Top Contributor
I tried it already.
I put
Expand|Select|Wrap|Line Numbers
  1. Array2D<char> myArray;
  2.  
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.
Expand|Select|Wrap|Line Numbers
  1.     Screen(): Xsize(80) , Ysize (23), myArray (Xsize,Ysize){};
  2.     ~Screen(){}
  3.  
and put myArray under private:
Expand|Select|Wrap|Line Numbers
  1.         private:
  2.         unsigned Xsize;
  3.         unsigned Ysize;
  4.         Array2D <char> myArray;
  5.         string status;
  6.  
Dec 11 '06 #5
DeMan
1,806 Top Contributor
<removed>
Sorry, as always I was beaten whilst I typed
Dec 11 '06 #6
nickyeng
254 Contributor
thank you Horace1.

it helped.

thanks again.
:D

Expand|Select|Wrap|Line Numbers
  1. Screen(): Xsize(80) , Ysize (23), myArray (Xsize,Ysize){};
  2.  
This is made me confused.
I have to get learn of it.

NickyEng.
Dec 11 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
6
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...
9
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...
2
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...
0
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...
2
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...
0
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...
4
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...
6
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...
6
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... ...
0
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...
0
Oralloy
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,...
1
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...
0
agi2029
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,...
1
isladogs
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...
0
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.