码迷,mamicode.com
首页 > 其他好文 > 详细

关于模版子类初始化模版父类,调用父类成员问题(未解决)

时间:2017-04-13 21:57:34      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:ted   parent   his   protected   漂亮   sys   logs   stream   name   

#include<iostream>
using namespace std;

template <typename T>
class Parent
{
public:
	Parent(T p1)
	{
		this->p1 = p1;
	}
	void printP()
	{
		cout << "p1==" << p1 << endl;
	}
protected:
	T p1;
};
template <typename U>
class Child : public  Parent <U>//这里的<>在parent前后都可以
{
public:
	Child(U c1, U tem) :Parent <U> (tem)
	{
		this->c1 = c1;
		
	}
	void printC()
	{
		cout << " c1 = " << c1 << " tem = " << tem << endl;//不理解这里为什么会出现错误?
	}
private:
	U c1;
};

class B:public Parent<int>
{
public:
	B(int b1, int p):Parent(p)
	{
		this->b1 = b1;
	}
	void printB()
	{
		cout << " b1 = " << b1 << " p1 = " << p1 << endl;
	}
private:
	int b1;

};
int main()
{
	Child<int> h1(1, 3);
	h1.printC();
	h1.printP();
	cout << " 我是漂亮的分割线\n";
	B b1(2,5);
	b1.printB();
	b1.printP();
	cout << " 我是漂亮的分割线\n";
	Parent <char> a1(‘d‘);
	a1.printP();

	system("pause");
}

  结果显示: error C2065: “tem”: 未声明的标识符

关于模版子类初始化模版父类,调用父类成员问题(未解决)

标签:ted   parent   his   protected   漂亮   sys   logs   stream   name   

原文地址:http://www.cnblogs.com/xiaochige/p/6706356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!