代码求助
  • 板块灌水区
  • 楼主botao0508
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/5/17 10:12
  • 上次更新2023/10/28 01:16:03
查看原帖
代码求助
640389
botao0508楼主2022/5/17 10:12

我自己编程遇到了点问题,一个模板类的模板构造函数访问不了参数的私有变量...... 这是简化的说明代码:

#include<iostream>
using namespace std;
template<typename T>
class A{
	public:
		A(T xs){
			x=xs;
		}
		template<typename U>
		A(const A<U>& xs){
			x=xs.x;
		}
		print(){
			cout<<x<<endl;
		}
	private:
		T x;
};
int main(){
	A<char> a('x');
	A<int> b(a);
	b.print();
	return 0;
}

编译会报错: main.cpp In instantiation of 'A::A(const A&) [with U = char; T = int]': 22 12 main.cpp required from here 18 5 main.cpp [Error] 'char A::x' is private 11 5 main.cpp [Error] within this context

可是明明是同一个类,为什么不能访问private... 哪位大佬给讲讲呗

2022/5/17 10:12
加载中...