I am writing code where I have three header files: figure, triangle, and rectangle. Triangle and rectangle classes inherit the figure class. I have all the functions defined in the class declarations and don't need an implementation file. When I compile, I get this error: error C2011: 'Figure' : 'class' type redefinition. Below is one of the header files for one of the derived classes, the others are more or less identical.

What is the proper method of setting this up?

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

using namespace std;

class Rectangle: public Figure
{
public:
	Rectangle(){cout << "Rectangle Constructor" << endl;}
	~Rectangle(){cout << "Rectangle Destructor" << endl;}
	void erase(){cout << "Rectangle Erase" << endl;}
	void draw(){cout << "Rectangle Draw" << endl;}
	void center(){cout << "Rectangle Center" << endl;}
};

Chances are we don't know what the problem is without seeing the code.

#include <iostream>

using namespace std;

class Figure
{
public:
	Figure(){cout << "Figure Constructor" << endl;}
	~Figure(){cout << "Figure Destructor" << endl;}
	void erase(){cout << "Figure Erase" << endl;}
	void draw(){cout << "Figure Draw" << endl;}
	void center(){cout << "Figure Center" << endl;}
};
#include <iostream>
#include "figure.h"
#include "rectangle.h"
#include "triangle.h"

using std::cout;

int main()
{
	Triangle tri;
	tri.draw();
	cout << "\nDerived class Triangle object calling center().\n";
	tri.center();

	Rectangle rect;
	rect.draw();
	cout << "\nDerived class Rectangle object calling center().\n";
	rect.center();
	return 0;
}

This error occurs because you are including a certain file multiple times and therefore trying to redefine the Figure class.

I've always been taught to wrap my class header files in the following format to prevent multiple definitions:

#ifndef __IN_HEADERNAME
#define __IN_HEADERNAME

  //
  // The original header goes here
  //
#endif

>#ifndef __IN_HEADERNAME
>#define __IN_HEADERNAME

The rules for leading underscores in names can be tricky, and it's easy to stomp all over the implementation's reserved identifiers. Unless you're intimately familiar with the rules (which you're clearly not), I'd recommend not using any leading underscores at all.

Yup adding the #ifndef worked, thanks! Taking the second level programming class at a different school so i think im missing out on some things.

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.