Reputation: 43
rectangleTypeTest.cpp
#include <iostream>
#include "rectangleType.cpp"
int main(){
rectangleType firstRectangle;
rectangleType secondRectangle(2, 2);
firstRectangle.setDimension(3, 3);
cout << "rectangle's length is: " << firstRectangle.getLength() << endl;
cout << "rectangle's width is: " << firstRectangle.getWidth() << endl;
cout << "rectangle's area is: " << firstRectangle.area() << endl;
cout << "rectangle's perimeter is: " << firstRectangle.perimeter() << endl;
secondRectangle.print();
}
rectangleType.cpp
#include<iostream>
#include"rectangleType.h"
using namespace std;
void rectangleType:: setDimension(double l, double w){
length = l;
width = w;
}
double rectangleType:: getLength() const{
return length;
}
double rectangleType:: getWidth() const{
return width;
}
double rectangleType:: area() const{
return (length*width);
}
double rectangleType:: perimeter() const{
return ((length*2)+(width*2));
}
void rectangleType:: print() const{
cout << "the width is: " << width << endl;
cout << "the length is: " << length << endl;
}
rectangleType:: rectangleType(){
length = 0;
width = 0;
}
rectangleType:: rectangleType(double l, double w){
length = l;
width = w;
}
rectangleType.h
class rectangleType
{
public:
void setDimension(double l, double w);
//Function to set the length and width of the rectangle.
//Postcondition: length = l; width = w;
double getLength() const;
//Function to return the length of the rectangle.
//Postcondition: The value of length is returned.
double getWidth() const;
//Function to return the width of the rectangle.
//Postcondition: The value of width is returned.
double area() const;
//Function to return the area of the rectangle.
//Postcondition: The area of the rectangle is
// calculated and returned.
double perimeter() const;
//Function to return the perimeter of the rectangle.
//Postcondition: The perimeter of the rectangle is
// calculated and returned.
void print() const;
//Function to output the length and width of
//the rectangle.
rectangleType();
//Default constructor
//Postcondition: length = 0; width = 0;
rectangleType(double l, double w);
//Constructor with parameters
//Postcondition: length = l; width = w;
private:
double length;
double width;
};
Above is my three code files. i am compiling using
g++ -o rectangleTypeTest rectangleTypeTest.cpp rectangleType.cpp
I am getting multiple definitions of 'rectangleType::setDimension(double, double)' And then the same error for every function and twice for the constructors. I assume I am doing something stupid, please advise. Thanks.
Upvotes: 0
Views: 313
Reputation: 904
Instead of including rectangleType.cpp
you should include rectangleType.h
, because your rectangleType.cpp
further includes rectangleType.h
which is causing this problem.
#include <iostream>
#include "rectangleType.h"
int main(){
rectangleType firstRectangle;
rectangleType secondRectangle(2, 2);
firstRectangle.setDimension(3, 3);
cout << "rectangle's length is: " << firstRectangle.getLength() << endl;
cout << "rectangle's width is: " << firstRectangle.getWidth() << endl;
cout << "rectangle's area is: " << firstRectangle.area() << endl;
cout << "rectangle's perimeter is: " << firstRectangle.perimeter() << endl;
secondRectangle.print();
}
Upvotes: 3