Ask Your Question
0

error: uninitialized local "variable" blean used

asked 2019-01-23 09:32:50 -0500

Allaye gravatar image
header file---------

class Blending
{
public:
    static void Blend(int, void*);
    int Blended();
    //int imageinput();
     double alpha, beta;
     int  key;
     int defaultvalue;
};


    cpp file ----------
        Mat  img2;
        double alpha, beta;
        int key;
        int maximumvalue = 100;
        int defaultvalue = maximumvalue / 2;

        void Blending::Blend(int, void*)
        {

            Blending blean;
            blean.alpha = (double)blean.defaultvalue / 100;
            addWeighted(img, blean.alpha, img2, 1.0 - blean.alpha, 0, des);
            imshow("Blendimage", des);
        }
    int main()
    {

        createTrackbar("Blend", "Blendimage", &defaultvalue,maximumvalue,Blending::Blend);

    }

blend is a static function and when i use the object of the class to access the variable of the class i get an uninitialized variable error any help... the variable are non-static variable

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2019-01-23 09:51:51 -0500

sjhalayka gravatar image

updated 2019-01-23 10:05:01 -0500

You need to add a constructor for your Blending class:

class Blending
{
public:
    Blending(void)
    {
        alpha = beta = key = defaultvalue = 0;
    }

// ...

};

Please upvote my answer, and mark it as correct, if it helped you. Thank you.

I also recommend that you buy the book The C++ Programming Language by Stroustrup.

Now, can I ask why you have duplicate variables outside of the class?

edit flag offensive delete link more

Comments

can you please explain why having a constructor is important.

Allaye gravatar imageAllaye ( 2019-01-23 10:12:11 -0500 )edit

The class's member variables aren't generally set to 0 when the class is instantiated, so you must do that manually in a constructor.

sjhalayka gravatar imagesjhalayka ( 2019-01-23 10:16:42 -0500 )edit
1

okay thanks, i commented out the constructor and initialized the variable in the header file, and it work, thanks alot

Allaye gravatar imageAllaye ( 2019-01-23 10:23:28 -0500 )edit

the constructor is initializing the alpha and beta variable to zero, making the blending not to work.

Allaye gravatar imageAllaye ( 2019-01-23 14:13:26 -0500 )edit

@Allaye -- You can initialize the alpha and beta to whatever you like in the constructor, it need not be all zeroes.

For example:

Blending(void)
{
    alpha = 0;
    beta = 0;
    key = 0;
    value = 50;
}

// ...

Blending blending_instance;

// ...

createTrackbar("Blend", "Blendimage", &blending_instance.value, 100, Blending::Blend);
sjhalayka gravatar imagesjhalayka ( 2019-01-23 15:22:30 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-01-23 09:32:50 -0500

Seen: 59 times

Last updated: Jan 23 '19