简体   繁体   English

C ++多定义错误

[英]C++ Multiple Definition Errors

rectangleTypeTest.cpp squareTypeTest.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 angleType.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 squareType.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. 我得到了'rectangleType :: setDimension(double,double)'的多个定义,然后每个函数都出现相同的错误,而构造函数则出现两次错误。 I assume I am doing something stupid, please advise. 我以为我做的事很愚蠢,请指教。 Thanks. 谢谢。

Instead of including rectangleType.cpp you should include rectangleType.h , because your rectangleType.cpp further includes rectangleType.h which is causing this problem. 而不是包括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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM