![]() |
#2
peach54602013-04-03 20:38
|
有道题大概是说什么求矩形的周长和面积,用类去做
我写了三个文件,一个.h,两个.cpp
但是编译时老是说undefined reference to ' ' 看了很久依然没有办法....求各位大神指教!
这是头文件

//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
public:
Rectangle(float = 1,float = 1);
void perimeter(float ,float );
void area(float ,float );
float getLength();
float getWidth();
void setLengthWidth(float ,float );
private:
float length;
float width ;
};
#endif
这是第一个cpp

// Rectangle.cpp
#include <iostream>
#include <stdexcept>
#include "Rectangle.h"
using namespace std;
Rectangle::Rectangle(float a,float b)
{
setLengthWidth(a,b);
}
void Rectangle::perimeter(float a,float b)
{
cout << "The perimeter of the rectangle is " << 2(a+b) << endl;
}
void Rectangle::area(float a,float b)
{
cout << "The area of the rectangle is " << a*b << endl;
}
float Rectangle::getLength()
{
return length;
}
float Rectangle::getWidth()
{
return width;
}
void Rectangle::setLengthWidth(float a,float b)
{
if((a>=0.0&&a<=20.0)&&(b>=0.0&&b<=20.0))
{
length = a;
width = b;
}
else
throw invalid_argument("The length or the width is out of range");
}
这是主函数的cpp

// RectangleClass.cpp
#include <iostream>
#include "Rectangle.h"
using namespace std;
int main()
{
float a,b;
cout << "Please enter the length and width for the rectangle : ";
cin >> a >> b;
Rectangle Calculate(a,b);
Calculate.perimeter(Calculate.getLength(),Calculate.getWidth());
Calculate.area(Calculate.getLength(),Calculate.getWidth());
return 0;
}
在线等......
