I've made a shape base class along with a bunch of derived classes. I have an object array of derived objects and a loop to do the function but am getting this error:
error LNK2001: unresolved external symbol "public: virtual void __thiscall Shape::drawObject(void)" (?drawObject@Shape@@UAEXXZ)

Thought it might be a problem caused from defining the functions in the header or but don't see why. Even MSDN reference for it says to try defining the function in the header. Anyone see why?

//Main file with figure pointer to the derived class functions

#include <iostream>
#include "shape.h"
#include "rectangle.h"
#include "triangle.h"
#include "circle.h"
#include "square.h"

using std::cout;


int main()
{
	Shape *shapes[5];

	shapes[0] = &Triangle(Blue, 50, 50, 5.0, 10.0);
	shapes[1] = &Circle(Green, 30, 30, 5);
	shapes[2] = &Circle(Red, 50, 50, 10);
	shapes[3] = &Rectangle(Violet, 15, 15, 5, 10);
	shapes[4] = &Square(Orange, 0, 0, 7);

	for(int i = 0; i < 5; ++i)
	{
		shapes[i]->drawObject();
		cout << "Area of shape[" << i << "]: " << shapes[i]->getArea() << endl;
	}


	return 0;
}
//shape.h
//shape base class declaration header file
//virtual commented out for first run, uncommented on second run

#ifndef SHAPE_H
#define SHAPE_H

#include <iostream>

using namespace std;

enum Color{Red, Orange, Yellow, Green, Blue, Indigo, Violet};

class Shape
{
public:
	Shape():color(Yellow), x(0), y(0){};/*{cout << "Figure Default Constructor" << endl;}*/
	Shape(Color col, int X, int Y):color(col), x(X), y(Y){};
	Shape(const Shape& rhs):color(rhs.color), x(rhs.x), y(rhs.y){};
	virtual ~Shape(){};
	virtual void ResetLocation(int deltaX, int deltaY)
	{
		x = deltaX;
		y = deltaY;
	}
	virtual void ResetColor(Color col){color = col;}
	virtual double getArea() = 0;
	virtual void drawObject();

protected:
	Color color;
	int x;
	int y;
};

#endif
//triangle.h
//triangle class declaration header

#ifndef TRIANGLE_H
#define TRIANGLE_H

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

using namespace std;

class Triangle: public Shape
{
public:
	Triangle(): Shape(Orange, 0, 0), base(0), height(0){};
	Triangle(Color col, int X, int Y, float Base, float Height): Shape(col, X, Y), base(Base), height(Height){};
	Triangle(const Triangle& rhs): Shape(rhs.color, rhs.x, rhs.y), base(rhs.base), height(rhs.height){};
	~Triangle(){};
	void setDim(float Base, float Height)
	{
		base = Base;
		height = Height;
	}
	double getArea(){return base * height;}
	void drawObject(){cout << "Drawing Triangle" << endl;}
	const Triangle operator+(const Triangle& rhs)
	{
		float Height = height + rhs.height;
		float Base = base + rhs.base;
		return Triangle(Indigo, 3, 3, Base, Height);
	}

private:
	float base;
	float height;
};

#endif

Recommended Answers

All 3 Replies

I've made a shape base class along with a bunch of derived classes. I have an object array of derived objects and a loop to do the function but am getting this error:
error LNK2001: unresolved external symbol "public: virtual void __thiscall Shape::drawObject(void)" (?drawObject@Shape@@UAEXXZ)

Thought it might be a problem caused from defining the functions in the header or but don't see why. Even MSDN reference for it says to try defining the function in the header. Anyone see why?

//Main file with figure pointer to the derived class functions

#include <iostream>
#include "shape.h"
#include "rectangle.h"
#include "triangle.h"
#include "circle.h"
#include "square.h"

using std::cout;


int main()
{
	Shape *shapes[5];

	shapes[0] = &Triangle(Blue, 50, 50, 5.0, 10.0);
	shapes[1] = &Circle(Green, 30, 30, 5);
	shapes[2] = &Circle(Red, 50, 50, 10);
	shapes[3] = &Rectangle(Violet, 15, 15, 5, 10);
	shapes[4] = &Square(Orange, 0, 0, 7);

	for(int i = 0; i < 5; ++i)
	{
		shapes[i]->drawObject();
		cout << "Area of shape[" << i << "]: " << shapes[i]->getArea() << endl;
	}


	return 0;
}
//shape.h
//shape base class declaration header file
//virtual commented out for first run, uncommented on second run

#ifndef SHAPE_H
#define SHAPE_H

#include <iostream>

using namespace std;

enum Color{Red, Orange, Yellow, Green, Blue, Indigo, Violet};

class Shape
{
public:
	Shape():color(Yellow), x(0), y(0){};/*{cout << "Figure Default Constructor" << endl;}*/
	Shape(Color col, int X, int Y):color(col), x(X), y(Y){};
	Shape(const Shape& rhs):color(rhs.color), x(rhs.x), y(rhs.y){};
	virtual ~Shape(){};
	virtual void ResetLocation(int deltaX, int deltaY)
	{
		x = deltaX;
		y = deltaY;
	}
	virtual void ResetColor(Color col){color = col;}
	virtual double getArea() = 0;
	virtual void drawObject();

protected:
	Color color;
	int x;
	int y;
};

#endif
//triangle.h
//triangle class declaration header

#ifndef TRIANGLE_H
#define TRIANGLE_H

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

using namespace std;

class Triangle: public Shape
{
public:
	Triangle(): Shape(Orange, 0, 0), base(0), height(0){};
	Triangle(Color col, int X, int Y, float Base, float Height): Shape(col, X, Y), base(Base), height(Height){};
	Triangle(const Triangle& rhs): Shape(rhs.color, rhs.x, rhs.y), base(rhs.base), height(rhs.height){};
	~Triangle(){};
	void setDim(float Base, float Height)
	{
		base = Base;
		height = Height;
	}
	double getArea(){return base * height;}
	void drawObject(){cout << "Drawing Triangle" << endl;}
	const Triangle operator+(const Triangle& rhs)
	{
		float Height = height + rhs.height;
		float Base = base + rhs.base;
		return Triangle(Indigo, 3, 3, Base, Height);
	}

private:
	float base;
	float height;
};

#endif

I think the linker is looking for the function definition for drawObject in your base class. You have to provide default behavior in your base class, or set it to pure virtual like you did with getArea by =0.

absolutely genius, thanks!

and you may want to avoid using namespaces in header files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.