problem with class compile

I have created these classes and used code::block to compile them but there is a "undefined reference Rectangle::setWid()" "undefined reference Rectangle::setLen()"...
Please help.
Here is my codes:

#ifndef RECTANGLE_H
#define RECTANGLE_H


class Rectangle
{
private:
double len;
double wid;
public:
void setLen(double);
void setWid(double);
double getLen() const;
double getWid() const;
double getArea() const;
};

#endif // RECTANGLE_H

/*I saved these files in Rectangle folder.*/

#include "rectangle.h"
#include<cstdlib>
#include<cctype>
#include<iostream>

using namespace std;

void Rectangle::setLen(double l)
{
len = l;
}
void Rectangle::setWid(double w)
{
wid = w;
}
double Rectangle::getLen() const
{
return len;
}
double Rectangle::getWid() const
{
return wid;
}

/*I also saved these files in Rectangle folder.*/

#include<iostream>
#include "rectangle.h"
#include<cctype>
#include<cstdlib>

using namespace std;
int main()
{
Rectangle l;
l.setLen(3);
l.setWid(4);
cout << "The area is: " << l.getArea() << endl;

return 0;
}
but when I compiled, the message
"undefined reference Rectangle::setWid()" "undefined reference Rectangle::setLen()"...
showed up.

Please help.
thank you very much!


Last edited on
closed account (Dy7SLyTq)
your passing ints. you need to pass doubles. ie
I.setWid(4.0);
It's still not working. Thanks.
You have no implementation for Rectangle::getArea(). That would you would get an undefined reference for it. I don't see offhand why you would be getting an undefined reference for getLen() or getWid().

I'm presuming the nested main functions is a cut and paste error.
1
2
3
4
5
int main()
{
Rectangle l;
int main()
{


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.

@DTSCODE - the compiler will do a type conversion from an int to a double. Specifying an int won't cause an undefined reference.

Edit: You could be getting the undefined external because you did not include rectangle.cpp in your project, therefore it did not get compiled nor linked into your executable.

I compiled this with VS2010 and did not get undefined externals for either getWid() or getLen(). I did receive an undefined external for getArea() as noted above.
Last edited on
Topic archived. No new replies allowed.