C++ 多重定义错误

C++ Multiple Definition 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)' 的多个定义,然后每个函数都出现相同的错误,构造函数出现两次。我假设我在做一些愚蠢的事情,请指教。谢谢

您不应包含 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();
}