views:

604

answers:

2

Hi,

I found that handy opencv library and tried to program a simple imagemodifier with it using C. When I started to compile my code I got some errors, that are a bit confusing as they doesn't seem to make sense. This is my first time when programming with netbeans and first time I'm trying to make something else than those basic calculator etc. programs. Below is the code and the errors the compiler gives:

int iscolor = -1; /* Used to load the image as it is*/
IplImage* image = 0;

/* Load Image */
image = cvLoadImage(const char* 'test.jpg', iscolor); /* line 34 */

/*Modify image*/
...

/* Save Image */
int cvSaveImage(const char* 'modified.jpg', const CvArr* image); /* line 43 */

/*End*/

Compiler errors

main.c: In function ‘main’:

main.c:34: error: expected expression before ‘const’

main.c:34:37: warning: character constant too long for its type

main.c:34: error: too few arguments to function ‘cvLoadImage’

main.c:43:33: warning: character constant too long for its type

main.c:43: error: expected ‘;’, ‘,’ or ‘)’ before '\x2e6a7067'

These errors confuses me a bit, because

  1. referring to opencv c reference cvLoadImage takes just two arguments.
  2. how come character constant is too long?
  3. expected expression before 'const'? hummm... I don't get this at all
  4. expected ‘;’, ‘,’ or ‘)’ before '\x2e6a7067'... well I don't get this either

    So can somebody tell me what I'm doing wrong? Any help would be nice :)

A: 

You should learn some c programming language, grab a book or something...

proper lines would be :

34: image = cvLoadImage('test.jpg', iscolor);

43: int result = cvSaveImage('modified.jpg', image);

JC
Umm. You need double quotes, not single. See my reply, or a book :-).
Alok
I admit, that I'm not very experienced in programming. I have studied few books, but those books didn't really help as I didn't know exactly what to look for.
zaplec
+2  A: 

In C, single quotes are used to denote single (literal) character constants, and the resulting type of such constants is int. So, 'a' is OK, 'a1' is not, because that's two characters. C defines some escape sequences, for example '\n', which is treated as one character signifying a newline, or '\xff', which is the character with hexadecimal value 0xff. Since 'test.jpg' contains more than one character, the compiler is telling you that your literal character constant is too long. You want a string, which needs double quotes.

You need to put parentheses around a cast: (const char *)x is okay, const char * x is not (well it is okay if you are declaring x as a const char *). I think you added that cast because the compiler was warning you about "converting int to pointer without a cast". In general, unless you really know what you're doing, casting to silence the compiler is a very bad idea.

Based upon the above, Line 34 should be:

image = cvLoadImage("test.jpg", iscolor);

If you really want to cast "test.jpg" as const char *, do:

image = cvLoadImage((const char *)"test.jpg", iscolor);

Also, now you know how to fix this line:

int cvSaveImage(const char* 'modified.jpg', const CvArr* image);
Alok
Thank you very much, that helped a lot. Now I can go on testing and writing this little program.
zaplec
I think I should write a note next to my screen that "double quotes for strings and single for literals" as I have made that same mistake many times before and it seems I have learned nothing from it :)
zaplec