Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have some trouble with my program.I wrote two classes
class LoadImage
{
 public:
     ......
     int width;
     int height;
     Byte *imgData;
};

In the class above I load an image,and get the information about width, height of the image,I want to process the image in the following class;
class ProcessImage
{
 ....
};
void ProcessImage::onProcess()
{
  LoadImage img;
  int lwidth=img.width; // this is the wrong way I used!!
  ...
}

in the class ProcessImage, I will use the member variables in class LoadImage frequently,but after I load the image, the member variables in class LoadImage are all destroyed, how can I use these variables when I process the image?
Posted
Comments
Richard MacCutchan 5-May-13 11:38am    
You need to actually load an image into your LoadImage object, and initialise the variables with the relevant information. As it stands your code does nothing.
Maciej Los 5-May-13 12:29pm    
Please, see this: Image Class (System.Drawing)[^].
Ian A Davidson 5-May-13 20:37pm    
This is C++, not C#.
Maciej Los 6-May-13 1:33am    
My little mistake because of C++ tag. Simply change C++ tab to C# tab ;)

In the first place, don't access member variables of other classes directly. This is very bad practice. Your member variables should be private and you should declare methods (functions) to return the value. This prevents a caller from setting them to invalid values.

In the second place, why are the member variables "destroyed" when you reach that line? They shouldn't be. What is more likely is that you haven't initialised them.

You are using the default constructor to create the LoadImage object. Is this what you want, or do you want to specify a file path or a resource id, for example?

Do you have a default constructor that correctly initialises the member variables (probably to zero).

How about this, for example:
C++
class LoadImage
{
public:
    LoadImage()
    :   width(0),
        height(0),
        imgData(NULL)
    {
    }

    LoadImage
        (
        const std::string& filePath
        )
    :   width(0),
        height(0),
        imgData(NULL)
    {
        // load image from filePath into imgData and set height and width accordingly.
    }

    virtual ~LoadImage()
    {
        if (NULL != imgData)
        {
            // Free imgData according to how you initialised it
            // And set it back to null (not important in this case, but it is good practice)
            imgData = NULL;
        }
    }

    inline int GetHeight() const
    {
        return height;
    }

    inline int GetWidth() const
    {
        return width;
    }

    // ......

private:
    int width;
    int height;
    BYTE* imgData;
};
 
Share this answer
 
Comments
Angela2012 5-May-13 21:06pm    
Thank you for your help,but how can I get the variables I need?
LoadImage img;
int lwidth=img.GetWidth();//I know this is good practice,but is the result different from"int lwidth=img.width;"
Ian A Davidson 5-May-13 21:10pm    
The result depends on how you implement GetWidth. In my example above, it is the same since I simply return "width". However, you could choose to implement it differently, for example by getting the value from some bitmap header structure or something that your class manages. This is another reason to use methods rather than accessing variables directly.

Did you see what I said about initialisation? The variables are not destroyed by the time you reach the line on which you say they are. I am wondering why you think they would be. Are you sure you just haven't initialised them correctly?

Regards,
Ian.
Angela2012 5-May-13 23:12pm    
Thank you for your kind help.Best wishes!
Ian A Davidson 6-May-13 3:38am    
Have you resolved the problem and got it working?
Well done.
All the best,
Ian.
You declare LoadImage but you never create the associated object. You need to create the object so that you can access the member data, like:

LoadImage img = new LoadImage();
int lwidth = img.Width;

Otherwise you are attempting to access protected memory or it could even be some random value because its not initialized.
 
Share this answer
 
Comments
Ian A Davidson 5-May-13 20:15pm    
This is wrong for C++. (You are probably thinking C#?)
"img" isn't a pointer to a LoadImage object.
"LoadImage img;" is creating a LoadImage object - it will create an object of LoadImage using the default constructor. Whether this has been implemented correctly to initialise the members is another matter, and I doubt it actually loads an image (unless there is some default specified) ;)
Regards,
Ian.
Angela2012 5-May-13 23:13pm    
Thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900