C ++多重定义错误

时间:2015-02-18 05:37:52

标签: c++ compiler-errors

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;
};

以上是我的三个代码文件。我正在使用

进行编译
g++ -o rectangleTypeTest rectangleTypeTest.cpp rectangleType.cpp

我得到'rectangleType :: setDimension(double,double)'的多个定义然后每个函数都有相同的错误,构造函数则是两次。我假设我做了一些愚蠢的事,请指教。感谢。

1 个答案:

答案 0 :(得分:2)

您应该包含rectangleType.cpp,而不是包含rectangleType.h,因为rectangleType.cpp进一步包含导致此问题的rectangleType.h

#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();
}