-1

I get an error LNK2019 every time I try to compile. It tells me their is a unresolved external symbol. The functions look perfectly fine to me Error

Error   1   error LNK2019: unresolved external symbol "public: __thiscall     Rectangle::Rectangle(void)" (??0Rectangle@@QAE@XZ) referenced in function "public: __thiscall Square::Square(double,double,double)" (??0Square@@QAE@NNN@Z)    C:\Users\Mustafa Alkatat\Desktop\Shape\Shape\Square.obj Shape

Error   2   error LNK1120: 1 unresolved externals   C:\Users\Mustafa Alkatat\Desktop\Shape\Debug\Shape.exe  Shape

Shape.h

#ifndef SHAPE_H
#define SHAPE_H

class Point {
public:
  double x;
  double y;
};

class Shape {
protected:
  Point center;

public:
// constructors
 Shape();
 Shape(double x, double y);

// destructor
virtual ~Shape();

// methods
 virtual void print();
 virtual double getArea() = 0;
  virtual bool canCoverPoint(Point p) = 0;
 };

#endif

Circle.h

#ifndef CIRCLE_H
#define CIRCLE_H

#include "Shape.h"

class Circle : public Shape {
protected:
  double radius;

public:
// constructor
Circle(double x, double y, double r);

// destructor
virtual ~Circle();

// methods
virtual void print();
virtual double getArea();
virtual bool canCoverPoint(Point p);
};

#endif

Rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H

#include "Shape.h"

class Rectangle : public Shape {
protected:
  double length;  // the side parallel with x-axis
  double width;   // the side parallel with y-axis

public:
// constructors
Rectangle();
Rectangle(double x, double y, double l, double w);

// destructor
virtual ~Rectangle();

// methods
virtual void print();
virtual double getArea();
virtual bool canCoverPoint(Point p);
};

#endif

Square.h

#ifndef SQUARE_H
#define SQUARE_H

#include "Rectangle.h"

class Square : public Rectangle {
public:
// constructors
Square(double x, double y, double length);

// destructor
~Square();

// method
virtual void print();
virtual double getArea();
};

#endif

Shape.cpp

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

Shape::Shape(){}

Shape::Shape(double x = 0., double y = 0.) {}

Shape::~Shape() { }

void Shape::print() {

}

Rectangle.cpp

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

Rectangle::Rectangle(double x, double y, double l, double w)
{
  width = w;
  length = l;
}

Rectangle::~Rectangle() {}

void Rectangle::print() {
  std::cout << "Center: (" << center.x << "," << center.y << ")\n";
  std::cout << "Length: " << length;
  std::cout << "Width: " << width;
  std::cout << "Area: " << getArea();
}

double Rectangle::getArea() {
  return width * length;
}

bool Rectangle::canCoverPoint(Point p) {
  for (int i = 1; i < 3; i++)
    if (p.x <= length && p.y <= width)
        return true;
    else
        return false;
}

Circle.cpp

#define _USE_MATH_DEFINES

#include "Circle.h"
#include <iostream>
#include <cmath>

Circle::Circle(double x, double y, double r)
{
  radius = r;
}

Circle::~Circle(){}

void Circle::print() {
  std::cout << "Center: (" << center.x << "," << center.y << ")\n";
  std::cout << "Radius: " << radius;
  std::cout << "Area: " << getArea();
}

double Circle::getArea() {
  return 3.14 * ( radius * radius);
}

bool Circle::canCoverPoint(Point p) {
  for (int i = 1; i < 3; i++)
    if (p.x <= radius && p.y <= radius)
        return true;
    else
        return false;
}

Square.cpp

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

Square::Square(double x, double y, double length) {}
Square::~Square(){}

void Square::print() {
  std::cout << "Center: (" << center.x << "," << center.y << ")\n";
  std::cout << "Side: " << length;
}

double Square::getArea() {
  return length* length;
}

TestShape.cpp

#include <iostream>
#include "Shape.h"
#include "Rectangle.h"
#include "Circle.h"
#include "Square.h"

using namespace std;

int main() {
// Initialize an array of Shape pointers
Shape* shapes[3];
shapes[0] = new Circle(2, 2, 2);
shapes[1] = new Rectangle(4, 3.5, 4, 3);
shapes[2] = new Square(2.5, 2.5, 3);

// Initialize test points
Point points[3] = {{3, 3}, {1, 1}, {4, 4}};

for (int i = 0; i < 3; i++) {
  // Test print function
  cout << "Shape " << i << ": " << endl;
  shapes[i]->print();

// Test getArea function
  cout << "\tArea: " << shapes[i]->getArea() << endl;

// Test canCoverPoint function
  for (int j = 0; j < 3; j++)
  cout << "\tIs point (" << points[j].x << ", " << points[j].y << ") in Shape " << i << "? "
    << (shapes[i]->canCoverPoint(points[j]) ? "Yes" : "No") << endl;
 }

// TODO: free up memory 

return 0;
}
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Baum mit Augen Feb 14 '15 at 21:05
  • Please change the title into something meaningful someone with the same problem can find. And for the next time you post a *"Why is my code not working"*-question: You need to include an [SSCCE](http://www.sscce.org). – Baum mit Augen Feb 14 '15 at 21:07

2 Answers2

0

A constructor will always call the parent constructor. If you do not specify one, it will call the default constructor (no parameters).

The issue here is that your Square constructor cannot call the Rectangle default constructor because there is none, and you do not explicitly call the existing constructor.

Implement your Square constructor so it explicitly calls the Rectangle constructor, v.g.

Square::Square(double x, double y, double length) : Rectangle(x, y, length, length) {  }
SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

You are missing the definition of the default constructor for the Rectangle.

in Rectangle.h you have said there will be a defined constructor for Rectangle(); - > known by code as Rectangle(void);

This appears to just be a simple oversight, just remove the default constructor from the Rectangle.h.