zoukankan      html  css  js  c++  java
  • 关于模版子类初始化模版父类,调用父类成员问题(解决)以及运算符重载什么时候用

    #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 << " 我是漂亮的分割线
    ";
    	B b1(2,5);
    	b1.printB();
    	b1.printP();
    	cout << " 我是漂亮的分割线
    ";
    	Parent <char> a1('d');
    	a1.printP();
    
    	system("pause");
    }
    

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

     

      

    #include<iostream>
    using namespace std;
    
    template <typename T>
    class Parent
    {
    public:
    	Parent(T p1=0)
    	{
    		this->p1 = p1;
    	}
    	void printP()
    	{
    		cout << "p1==" << p1 << endl;
    	}
    	friend ostream & operator << (ostream &out, Parent & e1);
    	
    protected:
    	T p1;
    };
    ostream & operator<<(ostream &out, Parent<int> & e1)
    {
    	out << e1.p1;
    	return out;
    }
    template <typename U>
    class Child : public  Parent <U>//这里的<>在parent前后都可以
    {
    public:
    	Child(U c1, U tem) :tem (tem)
    	{
    		this->c1 = c1;
    		
    	}
    	void printC()
    	{
    		//因为这里的tem是父类的对象,打印对象的时候应该是tem。什么什么,之类的而不是直接打印tem
    
    		cout << " c1 = " << c1 << " tem = " << tem << endl;//不理解这里为什么会出现错误?
    还是存在疑问,这里什么时候能够直接调用tem参数呢,而不是对象? } private: U c1; Parent<U> tem; }; //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 << " 我是漂亮的分割线 "; /*B b1(2,5); b1.printB(); b1.printP();*/ cout << " 我是漂亮的分割线 "; Parent <char> a1('d'); a1.printP(); system("pause"); }

      

    #include<iostream>
    using namespace std;
    
    template <typename T>
    class Parent
    {
    public:
    	Parent(T p1)
    	{
    		this->p1 = p1;
    	}
    	void printP()
    	{
    		cout << "p1==" << p1 << endl;
    	}
    	friend ostream & operator << (ostream &out, Parent & e1);
    	
    public:
    	T p1;
    };
    ostream & operator<<(ostream &out, Parent<int> & e1)
    {
    	out << e1.p1;
    	return out;
    }
    template <typename U>
    class Child : public  Parent <U>//这里的<>在parent前后都可以
    {
    public:
    	Child(U c1, U tem) :Parent<U> (tem)
    	{
    		this->c1 = c1;
    		//this->p1 = tem;
    	}
    	void printC()
    	{
    		//这个是傻子,你给p1赋值为tem,tem相当于一个字面量,怎么可以输出一个字面量呢?昨晚肯定晕了。
    		//cout << " c1 = " << c1 << " tem = " << tem << endl;//不理解这里为什么会出现错误?
    		cout << " c1 = " << c1 << " p1 = " << p1 << endl;
    		
    	}
    private:
    	U c1;
    	
    };
    
    class B :public Parent<int>
    {
    public:
    	B(int b1, int p) :Parent<int>(p)
    	{
    		this->b1 = b1;
    	}
    	void printB()
    	{
    		cout << " b1 = " << b1 << " p1 = " << p1 << endl;
    	}
    private:
    	int b1;
    
    };
    class Test
    {
    public:
    	Test(int t1)
    	{
    		this->t1 = t1;
    	}
    	void printT()
    	{
    		cout << t1 << endl;
    	}
    public:
    	int t1;
    };
    int main()
    {
    	Child<int> h1(1, 3);
    	cout<<h1.p1;
    	h1.printC();
    	h1.printP();
    	cout << " 我是漂亮的分割线
    ";
    	B b1(2,5);
    	cout<<b1.p1;
    	b1.printB();
    	b1.printP();
    	cout << " 我是漂亮的分割线
    ";
    	Parent <char> a1('d');
    	a1.printP();
    	cout << " 我是漂亮的分割线
    ";
    	Test t(2);
    	cout<<t.t1;
    	system("pause");
    
    }
    

      

  • 相关阅读:
    聊聊WS-Federation
    用双十一的故事串起碎片的网络协议(上)
    责任链模式的使用-Netty ChannelPipeline和Mina IoFilterChain分析
    最小化局部边际的合并聚类算法(中篇)
    最小化局部边际的合并聚类算法(上篇)
    UVaLive 7371 Triangle (水题,判矩形)
    UVaLive 7372 Excellence (水题,贪心)
    POJ 3312 Mahershalalhashbaz, Nebuchadnezzar, and Billy Bob Benjamin Go to the Regionals (水题,贪心)
    UVa 1252 Twenty Questions (状压DP+记忆化搜索)
    UVa 10817 Headmaster's Headache (状压DP+记忆化搜索)
  • 原文地址:https://www.cnblogs.com/xiaochige/p/6706356.html
Copyright © 2011-2022 走看看