Click here to Skip to main content
15,888,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I write 3 class with template:

class Parent
class SWAP
class insertionSort : public SWAP<t> , public Parent<t>

when I complie code , compiler return this error:

Error 1 error C2512: 'Parent<int>' : no appropriate default constructor available

Code :

C++
// searches-sorts.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>

using namespace std;

template <class T>
class Parent
{
public:
	T *arr;
	int len;
	virtual void Sort();
	
	

	Parent(T *a, int l)
	{
		len = l;
		arr = new T[l];
		for (int i = 0; i < l; i++)
		{
			arr[i] = a[i];
		}
	}
};


//Swap
template <class T>
class SWAP
{
public:
	void swap(T *a, int l ,int i, int j)
	{
		T temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
};


//insertion Sort
template <class T>
class insertionSort : public SWAP<T> , public Parent<T>
{
public:
	insertionSort(T *a, int l)
	{
		Parent::Parent(a, l);
	}

	void Sort()
	{
		int j;

		for (int i = 0; i < len; i++)
		{
			j = i;
			while (j > 0 && arr[j] < arr[j - 1])
			{
				SWAP::swap(arr, len,j, j - 1);
				j--;
			}
		}
	}
};


//
int main( )
{
	int *a = new int[5];
	for (int i = 0; i < 5; i++)
	{
		a[i] = i;
	}
	a[3] = 10;

	Parent<int> *ex = new insertionSort<int>(a, 5);
	

	for (int i = 0; i < 5; i++)
	{
		cout << a[i] << ", ";
	}

	return 0;
}
Posted
Updated 21-Nov-14 1:14am
v3

As it says in the error message, where's your default constructor for parent?

If the compiler doesn't see an explicit reference to a base class constructor in the derived class initialiser list it'll try and generate a call to the base class default constructor.

To avoid this you need something like:

C++
insertionSort(T *a, int l) : Parent(a, l)
{
}


and not the syntax you used.

Incidentally should SWAP be a public base class of insertionSort? What sort of operations does SWAP provide that clients of insertionSort want to use? Looking at the lack of data members are you even sure that SWAP is a class and not a function? Actually the entire thing looks like a bunch of functions rather than being classes but maybe that's me.
 
Share this answer
 
Comments
farhad bat 21-Nov-14 7:19am    
I know swap must be function but my professor insist write it as class!!
Aescleal 21-Nov-14 8:27am    
I'd suggest getting a new professor - unless there's something very subtle he's trying to demonstrate he's not got a clue about how to use C++
I solve it

C++
// searches-sorts.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>

using namespace std;

template <class t="">
class Parent
{
public:
	T *arr;
	int len;
	virtual void Sort()
	{
	}
	

	Parent(T *a, int l)
	{
		len = l;
		arr = new T[l];
		for (int i = 0; i < l; i++)
		{
			arr[i] = a[i];
		}
	}

	void print()
	{
		for (int i = 0; i < len; i++)
		{
			cout << arr[i] << ", ";
		}
		cout << endl;
	}
};


//Swap
template <class t="">
class SWAP
{
public:
	SWAP()
	{
	}
	void swap(T *a, int l ,int i, int j)
	{
		T temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
};


//insertion Sort
template <class t="">
class insertionSort : public SWAP<t> , public Parent<t>
{
public:
	insertionSort(T *a, int l)
		:SWAP(),Parent(a, l)
	{
	}

	void Sort()
	{
		int j;

		for (int i = 0; i < len; i++)
		{
			j = i;
			while (j > 0 && arr[j] < arr[j - 1])
			{
				SWAP::swap(arr, len,j, j - 1);
				j--;
			}
		}
	}
};


//
//
int main( )
{
	int *a = new int[5];
	for (int i = 0; i < 5; i++)
	{
		a[i] = i;
	}
	a[3] = 10;

	Parent<int> *ex = new insertionSort<int>(a, 5);
	ex->Sort();
	ex->print();

	

	return 0;
}


</int></int></t></t></class></class></class></iostream>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900