So I have a "base" class called "SortData". When I try to allow my other header file "InsertionSort.h" inherit this base class, I keep getting a class type redefinition error. What am I doing wrong?

Here's the base class header:

// SortData.h

#include <iostream>

using namespace std;

class SortData
{
public:
	SortData(int max=100);
	~SortData(void);

	int size() const;
	void randomize(int seed = 1);
	void printSome(const int num=10) const;

	virtual _int64 sort() = 0;

protected:
	long *theData;
	int maxSize;
};

And here's the header that's trying to inherit this base class:

// InsertionSort.h

#include "SortData.h"

class InsertionSort : public SortData
{
public:
	InsertionSort(int max=100);
	~InsertionSort(void);
	_int64 sort();

private:
	_int64 numops;
	void insertionSort(long theArray[], int n);
};

Dani AI

Generated

The redefinition was caused by the header being pulled into the translation unit more than once; 's suggestion to add header guards (or use #pragma once) is the right fix. After that, consider a few small improvements that prevent other common pitfalls in base/derived designs and make the code more portable and safer over time.

A compact, modernized sketch for the base class showing the key ideas (no raw using namespace std in headers, portable integer type, RAII-managed storage, and a virtual destructor):

#pragma once
#include <cstdint>
#include <cstddef>
#include <vector>

class SortData {
public:
    explicit SortData(std::size_t capacity = 100);
    virtual ~SortData() noexcept = default;   // virtual so deleting via base is safe

    virtual std::int64_t sort() = 0;

protected:
    std::vector<long> data_;                  // automatic memory management
};

Extra notes and troubleshooting tips:

  • Do not put using namespace std; in a header — it pollutes every file that includes it. Use fully qualified names instead.
  • If you keep raw pointers, follow the Rule of Three/Five: implement or delete copy ctor/assignment, or prefer std::vector/std::unique_ptr to avoid manual management.
  • _int64 is MSVC-specific; prefer std::int64_t from <cstdint> for portability.
  • If you see new redefinition errors after adding guards, check for accidental multiple definitions (e.g., defining a function in a header without inline) or circular includes; move implementations to .cpp and use forward declarations where possible.

These changes keep the original fix suggested by but also harden the design so (and future readers) avoid subtle runtime and portability issues.

Recommended Answers

All 2 Replies

Are you including both in main.cpp? This could be your problem and the solution is to use preprocessor blockers

SortData.h

#ifndef SORTDATA_H
#define SORTDATA_H

class SortData
{


};

#endif

And do the same thing for InsertionSort.h but with a different blocker name

Yeah I just realized that...sorry for the waste of forum space :(

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.