无法弄清楚我的代码是什么问题

时间:2015-02-14 21:02:48

标签: c++

每次尝试编译时都会收到错误LNK2019。它告诉我他们是一个未解决的外部符号。这些功能看起来非常好 错误

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

2 个答案:

答案 0 :(得分:0)

构造函数将始终调用父构造函数。如果您没有指定一个,它将调用默认构造函数(无参数)。

这里的问题是你的Square构造函数无法调用Rectangle默认构造函数,因为没有,并且你没有显式调用现有的构造函数。

实施您的Square构造函数,以便显式调用Rectangle构造函数,v.g。

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

答案 1 :(得分:0)

您缺少Rectangle的默认构造函数的定义。

在Rectangle.h中你已经说过将会有一个定义的Rectangle()构造函数; - &gt;代码称为Rectangle(void);

这似乎只是一个简单的疏忽,只需从Rectangle.h中删除默认构造函数。