Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

class declaration

hey again,

i need to declare a class in a section in my program which would give it 'block scope' ? basically i declare it in an IF statement. later further down the function (outside of the block) when i try to access it, i get an error saying "'xxxx' : undeclared identifier".

i've used:
Image *iImage = new Image(iImageWidth, iImageHeight);

...to declare the instance of the class, then:
iImage.Print(stBmpFile);

...to try and use it. i also get a 'left of '.Print' must have class/struct/union' error as well. im pretty sure i have the whole variable scope understanding down, but this has me scratching my head.

appreciate any help :)
0
gem56
Asked:
gem56
  • 7
  • 6
  • 4
  • +1
2 Solutions
 
jkrCommented:
If you declare a pointer like

Image *iImage = new Image(iImageWidth, iImageHeight);

You need to use the indirevtion operator:

iImage->Print(stBmpFile);
0
 
Infinity08Commented:
>> basically i declare it in an IF statement. later further down the function (outside of the block) when i try to access it, i get an error saying "'xxxx' : undeclared identifier".

        if (condition) {
            Image iImage(iImageWidth, iImageHeight);
            iImage.Print(stBmpFile);
        }
        // you can't use the iImage object here any more

Is that what you wanted ?
0
 
gem56Author Commented:
thanks guys.

ill give the pointer thing a try, i've overloaded my brain with c++ so much that i cant even remember the basics.

i can access the pointer in these forms:
iImage->Print(stBmpFile);
...or...
(*iImage).Print(stBmpFile);
right?

anyway the aim is that i need to use it (and pass it to functions) OUTSIDE of the code block that i declared it in. i take it i can keep using the pointer right?

also im useless with pointers, but when i need to pass this to functions i would use:
(for function declaration) void Function(Image *iImage) {.....}
(for passing into function) Function(*iImage);
(for using in function) iImage->Print(stBMPFile);

is this correct?

cheers
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
Yes, both would work. If you have

void Function(Image *iImage) {.....}

you are to pass apointer, i.e.

Function(iImage);

and not

Function(*iImage);

which would be required for a function that takes an instance and not a pointer.

0
 
gem56Author Commented:
awesome, thanks for that.

secondly, im having problems access the pointer. i declared the object using: 'Image *iImage = new Image(iImageWidth, iImageHeight);' The declaration was made within an IF statement, so it would have block scope right? however i thought that with the '= new Image' it would have global scope (unless its deleted) ?

i've tried using 'iImage->Print(stBmpFile);' to access the object further down in my code (outside the block) and im still getting the "'iImage' : undeclared identifier" error :S
0
 
JimBrandleyCommented:
You could do it this way:
Image *iImage = NULL;
if (condition)
{
   *iImage = new Image(iImageWidth, iImageHeight);
   ...
}
...
if (*iImage)
{
   /* Do something with it. */
}

Jim
0
 
gem56Author Commented:
hey Jim,

wow slow down champ, what does 'if (*iImage)' check for? ......only have a very basic understanding of this language :(
0
 
jkrCommented:
The scope of the variable must match, i.e. if 'iImage' is declared inside a statement like 'if' or 'for', it will not be valid outside these. Be sure to declare the variable at function scope.
0
 
JimBrandleyCommented:
That evaluates to true if the pointer is not null.

Jim
0
 
gem56Author Commented:
ok, um, crap.

ok my program only needs ONE instance of this object. so in my function scope i've declared:

Image *iImage;

then in my block where i initialize it, i have:

Image *iImage = new Image(iImageWidth, iImageHeight);

...and when i try and use the object with:

iImage->Print(stBmpFile);

my program crashes. obviously im not doing something right. i gather that when i declare it in function scope im making and instance, then when i '= new Image' in block scope, i seem to be warping it or something because when i use it, i no longer have the same instance of the object.

ok so how would i go about the follow: looks like i need to give it function scope because i need to use it outside of the code block i create an instance of it. i also need to pass the same object to functions.

thanks
0
 
jkrCommented:
Could you post the complete code?
0
 
gem56Author Commented:
all 220 lines? :P

ok the useful parts are:

// Start

int main()
{
      // Variable declaration
      Image *iImage;

..........

       // stringstream ssFileLine           // Holds one line from input text file, which is 'image BMP 300 300'
       // string stFileWord                     // Var holds word-by-word output from stream

      // Output first word in file/stream
      ssFileLine >> stFileWord;

      if (stFileWord == "image")
      {
            // If so, create new string var.
            string stBMP;

            // Output next word
            ssFileLine >> stBMP;

            // If next word is 'BMP'
            if (stBMP == "BMP")
            {

                  // Create two int vars to store canvas dimensions
                  int iImageWidth = 0;
                  int iImageHeight = 0;

                  // Output next two words into dimension vars
                  ssFileLine >> iImageWidth >> iImageHeight;

                  // Create new Image object with vars
                  Image *iImage = new Image(iImageWidth, iImageHeight);

            }
       }

........

      iImage->Print(stBmpFile);

      }

// End

'......' symbols are breaks in the code. all var's are declared, just commented to inform you what they are and hold. also i know i should have a huge main function, but my maids out and clean up duty is later :)

cheers
0
 
jkrCommented:
You need to check if 'iImage' is instantiated correctly and remove the local declaration inside the 'if' statement, i.e.

int main()
{
      // Variable declaration
      Image *iImage = NULL;

      if (stFileWord == "image")
      {
            // If so, create new string var.
            string stBMP;

            // Output next word
            ssFileLine >> stBMP;

            // If next word is 'BMP'
            if (stBMP == "BMP")
            {

                  // Create two int vars to store canvas dimensions
                  int iImageWidth = 0;
                  int iImageHeight = 0;

                  // Output next two words into dimension vars
                  ssFileLine >> iImageWidth >> iImageHeight;

                  // Create new Image object with vars
                  iImage = new Image(iImageWidth, iImageHeight); // <-----------!!

            }
       }

      if (iImage != NULL) iImage->Print(stBmpFile);
0
 
jkrCommented:
To explain a bit more, in

            if (stBMP == "BMP")
            {

                  // Create two int vars to store canvas dimensions
                  int iImageWidth = 0;
                  int iImageHeight = 0;

                  // Output next two words into dimension vars
                  ssFileLine >> iImageWidth >> iImageHeight;

                  // Create new Image object with vars
                  Image *iImage = new Image(iImageWidth, iImageHeight);

            }

you are declaring a variable that is *local* to the 'if' and therefore you are *not* initializing the variable that is used in

iImage->Print(stBmpFile);

but a different one.
0
 
JimBrandleyCommented:
JKR is correct, but just to explain the the other bit:
     Image *iImage;
versus:
     Image *iImage = NULL;

By delcaring the pointer, you are reserving some space on the stack. The first form does that, but the content of the address pointed to on the stack contains whatever was placed there the last time it was used. It is uninitialized. If you later test that value, it will likely not be null, and if not will definately be invalid. A null test may or may not pass.

The second form reserves the same space, but immediately initializes that memory to zeros. Now a null test will not pass.

Jim

0
 
gem56Author Commented:
ohh ok, so the

Image *iImage = NULL;

basically declares the object at function scope,

iImage = new Image(iImageWidth, iImageHeight);

initializes that object, while the

Image *iImage = new Image(iImageWidth, iImageHeight);

statement creates a new localized instance of that object... which was why i have faulty readings before when i tried to access it outside of the code block.

ok fantastic, hopefully this will stick in my head :P

thanks a lot guys, really appreciate the help... would have gone crazy daaaaays ago without it.
0
 
gem56Author Commented:
awesome, thanks for that explanation Jim :)
0
 
JimBrandleyCommented:
My pleasure. Good luck.

Jim
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now