Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

C++: undefined reference to Constructor

I just started working with C++ and am working on an exercise that deals with polymorphic pointers. I'm having trouble trying to solve an error message I believe I'm getting from my Rectangle.cpp as I call the class from my main.cpp.

The error message:

undefined reference to 'Rectangle::Rectangle(double, double)'

Main.cpp

#include <iostream>
#include "Rectangle.h"
using namespace std;

//////////////////////////////////////////////
//       --- FUNCTIONS DECLARATION---
void introduceShape(Shape*);
double calculateShapeArea(Shape*);
double calculateShapeCircumferece(Shape*);


int main()
{
    Rectangle rectangle1(5,2);
 //   Rectangle *rec1 = new Rectangle(5,2);

    introduceShape(&rectangle1);
    cout << "My area is: " << calculateShapeArea(&rectangle1) << ", my circumference is: " << calculateShapeCircumferece(&rectangle1) << endl << endl;


    return 0;
}

//////////////////////////////////////////////
//         --- FUNCTIONS ---
void introduceShape(Shape* shapeToIntroduce)
{
    return shapeToIntroduce->introduce();
}

double calculateShapeArea(Shape* shapeToCalculate)
{
    return shapeToCalculate->calculateArea();
}

double calculateShapeCircumferece(Shape* shapeToCalculate)
{
    return shapeToCalculate->calculateCircumference();
}

Rectangle.h

#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include "Shape.h"
#include <iostream>

using namespace std;

class Rectangle: public Shape
{
    double width;
    double height;
public:
    Rectangle(double , double );
    void introduce();
    double calculateArea();
    double calculateCircumference();

};

#endif // RECTANGLE_H_INCLUDED

Rectangle.cpp

#include "Rectangle.h"
#include <iostream>

using namespace std;

Rectangle::Rectangle(double width, double height)
{
    this->width = width;
    this->height = height;
}
void Rectangle::introduce()
{
    cout << "I AM A RECTANGLE !" << endl;
}
double Rectangle::calculateArea()
{
    return width*height;
}
double Rectangle::calculateCircumference()
{
    return 2*(width+height);
}

Shape.h

#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED

class Shape
{
    public:
        virtual void introduce() = 0;
        virtual double calculateArea() = 0;
        virtual double calculateCircumference() = 0;
};

#endif // SHAPE_H_INCLUDED

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error is generated by the linker because it can not see where the definition of the constructor is located.

If you are using an IDE, you should add .cpp file to the project so that it can be compiled and the definition would be found by the linker. It not, then you have to compile it yourself -assuming you are using gcc:

g++ Rectangle.cpp

will combine cpp files into one executable and should not show you that error. Visit this post


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...