tags:

views:

99

answers:

4

The Image class is a subclass of the BMP class. I am attempting to use some methods TellHeight(), TellWidth(), etc. from the BMP class in order to manipulate an image in this method. However, when I create a BMP and attempt to call functions on it, upon compilation I get an error saying

Undefined Symbols:

BMP::BMP(), referenced from:
Image::invertcolors()

This is the method invertcolors():

void Image::invertcolors()
{
BMP img_invert;
img_invert.ReadFromFile("inverted.bmp");
//int height =img_invert.TellHeight();
//int width = img_invert.TellWidth();

//for(int i = 0; i<height; i++)
//{
//for(int j =0; j<width; j++)
//{
//RGBApixel* current = img_invert(j,i);
//current->Blue = 255 - (current->Blue);
//current->Green = 255 - (current->Green);
//current->Red = 255 - (current->Red);
//current->Alpha = 255 - (current->Alpha);
//}
//}
}
+1  A: 

BMP does not have a no-argument constructor, you must use another means to get an instance of BMP to use.

caskey
If BMP does not have a default constructor, then you'd get a compile time error, not a link error.
R Samuel Klatchko
+1  A: 

Your BMP class does not have a default constructor. Either implement one, or make Image call its parent's initializer properly. For example,

// this class does not have a default constructor:
class Parent 
{
public:
   Parent( int a ) { /* .. */ }
};


// so this one must call its parent's constructor with a parameter
class Child : public Parent
{
public:
   Child() : Parent(42) { /* .. */ } 
}
Crashworks
If BMP does not have a default constructor, then you'd get a compile time error, not a link error.
R Samuel Klatchko
You're right that it's probably a link error, but FYI I *have* seen compilers that say "undefined symbol" when you call a function that isn't prototyped (or use a variable that's undeclared). Not every compiler uses GCC's exact error messages.
Crashworks
The BMP class does have a default constructor already implemented, so do you think that means there's a problem upon compilation because there is a linker error?
Arjun Nayini
Yes, if you see the constructor prototyped, this error means that the linker could not find the body for the function. This is most likely because that body is in a library. The library can be linked into your application by the means Klatchko describes. It is also possible that the definition is in a .CPP file that you have forgotten to include in your project/makefile.
Crashworks
A: 

looks more like a linker error. THe BMP library is not being linked in

pm100
+2  A: 

Undefined symbol is a link time error. When you are linking your binary, you need to link with the compiled code that defines that function.

If BMP is part of an external library, you'll need to add something like this to your link line:

 -L/path/to/lib -lbmp
R Samuel Klatchko
or if its in a separate .cpp file then add its .o. You didnt say what toolchain you are using so its hard to be specific
pm100