模板类构造函数出错 [英] Error in template class constructor

查看:201
本文介绍了模板类构造函数出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用模板写了3课:



class Parent

class SWAP

class insertionSort:public SWAP< T> ,公共父母< t>



当我编译代码时,编译器返回此错误:



错误1错误C2512:'Parent< int>':没有合适的默认构造函数



代码:



< pre lang =c ++> // searching-sorts.cpp:定义控制台应用程序的入口点。
//
#include stdafx.h
#include < < span class =code-leadattribute> iostream >

使用 命名空间标准;

模板< class T>
class
{
public
T * ARR;
int len;
virtual void Sort();



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


// 交换
模板< class T>
class SWAP
{
public
< span class =code-keyword> void swap(T * a, int l, int i, int j)
{
T temp = a [i];
a [i] = a [j];
a [j] = temp;
}
};


// 插入排序
template < class T>
class insertionSort: public SWAP< T> , public 父< T>
{
public
insertionSort(T * a, int l)
{
父母::父母(a,l);
}

void 排序()
{
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 ;

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


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

return 0 ;
}

解决方案

正如在错误消息中所说的那样,父母的默认构造函数在哪里?



如果编译器在派生类初始化列表中没有看到对基类构造函数的显式引用,它将尝试生成对基类默认构造函数的调用。



为了避免这种情况,你需要这样的东西:



 insertionSort(T * a,  int  l):父(a,l)
{
}





而不是你使用的语法。



顺便说一句,SWAP应该是insertSort的公共基类吗? SWAP提供什么类型的操作,insertSort的客户端想要使用?查看缺少数据成员您是否确定SWAP是一个类而不是函数?实际上整个事情看起来像是一堆函数而不是类,但也许就是我。


我解决了它



< pre lang =c ++> // searching-sorts.cpp:定义控制台应用程序的入口点。
//
#include stdafx.h
#include < < span class =code-leadattribute> iostream >

使用 命名空间标准;

template < class t = >
class
{
public
T * ARR;
int len;
虚拟 void 排序()
{
}


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


// 交换
template < class t = >
class SWAP
{
public
SWAP ()
{
}
void swap(T * a, int l, int i, int j)
{
T temp = A [1];
a [i] = a [j];
a [j] = temp;
}
};


// 插入排序
template < class t = >
class insertionSort: public SWAP< t> , public 父< t>
{
public
insertionSort(T * a, int l)
:SWAP(),父母(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 ;

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



return 0 ;
}


< / int>< / int>< / t>< / t>< / class>< / class>< /类>< /&的iostream GT;


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 :

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

解决方案

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:

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.


I solve it

// 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>


这篇关于模板类构造函数出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆