Ask Your Question

Mat img = imread(..) vs img = imread(..)

asked 2015-03-04 14:01:57 -0600

pingping__ gravatar image

i have img as a member in the .h file, but when i use

img = imread(..);

in the .cpp file, it crashes. however, using

Mat img = imread(..);

works. what is the difference? thanks!

note: opencv3.0 with qt

edit retag flag offensive close merge delete



"it crashes" IS NO ERROR MESSAGE! What exactly happens? Which message do you get?

FooBar gravatar imageFooBar ( 2015-03-05 04:18:17 -0600 )edit

Is your member in the header file declared public? Is it inside a namespace? The second declaration won't even use your global variable, but rather create a local img object withing the scope of your cpp file, but not update the value of your global img object.

However I am not sure if you can make a non static object in header files. Never done so before, only know the principle.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-03-06 02:48:20 -0600 )edit

@FooBar there werent any error messages... a window popped up saying it stopped running and that it is checking for a solution, and on my Qt creator IDE, it says after the program closed, this: The program has unexpectedly finished.

@StevenPuttemans yes it was in the header file, but in private instead. I tried putting it as public, but it was still the same... I actually dont want to make it a local variable, but it seems like it wont crash if i do it that way.. could it be that it wasnt initialized to something? not sure how this works.. :(

pingping__ gravatar imagepingping__ ( 2015-03-08 21:42:27 -0600 )edit

@pingping__ it seems to me that you need to dig deeper in C++ basics of how header and source code files work together. This seems to be not an OpenCV problem at all.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-03-09 04:49:13 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-03-06 01:32:08 -0600

juanmamdp gravatar image

In the code that gives you an error, img is defined within the scope of that variable?

edit flag offensive delete link more


Thats not what he wants. The reason of declaring public variables in header files is that you can use them over several cpp files...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-03-06 02:49:01 -0600 )edit

Question Tools

1 follower


Asked: 2015-03-04 14:01:57 -0600

Seen: 911 times

Last updated: Mar 04 '15