如何在类 A 中传递类型名 T 以在另一个类 B 中使用
How to pass typename T in class A to use in another class B
我对模板有问题。如何将typename T
传递到class A
以在class B
中使用它?我只想在class A
中传递类型名,但我也需要在class B
中使用它。可能吗?
#include <iostream>
using namespace std;
template <typename T>
class A {
public:
B getObj() { return obj; };
void setObj(B bObj) { obj = bObj; };
private:
B obj;
};
template <typename T>
class B {
public:
T getValue() { return value; };
void setValue(T val) { value = val; };
private:
T value = 0;
};
int main() {
A<int> a;
a.setObj(B()); // Is it possible to write B without typename every time?
cin.get();
return 0;
}
此代码编译时出现以下错误:
error C3646: 'getObj': unknown override specifier
note: see reference to class template instantiation 'A<T>' being compiled
error C2059: syntax error: '('
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
error C2061: syntax error: identifier 'B'
error C3646: 'obj': unknown override specifier
error C2059: syntax error: '='
error C2238: unexpected token(s) preceding ';'
fatal error C1903: unable to recover from previous error(s); stopping compilation
编辑。
简化代码:
#include <iostream>
using namespace std;
template <typename T>
class B {
public:
T value;
};
template <typename T>
class A {
public:
B obj;
};
int main() {
B<int> b; // can I simply write B b intead of B<int> b?
b.value = 9;
A<int> a;
a.obj = b;
cout << a.obj.value; // ?
cin.get();
return 0;
}
编译时出现错误:
error C2955: 'B': use of class template requires template argument list
note: see declaration of 'B'
note: see reference to class template instantiation 'A<T>' being compiled
error C2582: 'operator =' function is unavailable in 'B'
warning C4552: '<<': result of expression not used
即使没有B
也可以写它:
template<typename T>
struct B {};
template<typename T>
struct A {
void setObj(B<T> obj) {
obj_ = obj; // or obj_ = std::move(obj);
}
B<T> obj_;
};
int main() {
A<int> a;
a.setObj({});
}
除非B
的构造函数标记为 explicit
,否则这将起作用。
要引用 A<T>
中的B<T>
,可以引入类型别名:
template<typename T>
struct A {
using B = ::B<T>;
void setObj(B obj) {
obj_ = obj;
}
B obj_;
};
int main() {
using my_A = A<int>;
my_A a;
a.setObj(my_A::B{});
}
编辑后添加。
您不能省略模板参数,除非编译器可以以某种方式推断出它。在 C++17 中,我们有模板参数推导,您的代码可以编写为:
template<typename T>
struct B {
B(T v) : value(v) {}
T value;
};
template<typename T>
struct A {
A(B<T> o) : obj(o) {}
B<T> obj;
};
int main() {
B b{9};
A a{b};
std::cout << a.obj.value;
}
相关文章:
- 检查一个类型是否直接派生自"enable if"上下文中的另一个类型(是其子类型)
- C++将一个指针分配给另一个指针时执行的类型检查
- 如何包装一个函数以适应另一个函数的所需类型
- 在 C++ 中将一个模板类型的对象类型转换为另一个模板类型
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- 如何构造一个类型特征,可以判断一个类型的私有方法是否可以在另一个类型的构造函数中调用?
- 缺少类型说明符和另一个问题
- 如果一个变量在它之前释放了另一个(相同的数据类型)变量,如何将其分配给内存?
- 如何在类 A 中传递类型名 T 以在另一个类 B 中使用
- 将积分类型的数组作为另一个不相关的积分类型的阵列进行访问的安全且符合标准的方法
- c ++ 我是否需要手动删除指向另一个具体类型的 void* 指针?
- 如何在另一个类中调用类型类的向量?
- 使用 SFINAE 测试是否可以将一个指针类型static_cast到另一个指针类型
- 我可以使用预处理器将一个类型声明替换为另一个类型声明吗?
- 在函数模板中将一个类型名映射到另一个类型名
- 如何将数组的整数与另一个类型为"int"的变量进行比较?
- 如何检测一个类型是否是另一个类型的可见基
- 如何编写类型特征来检查一个类型是否可以通过非窄化转换转换为另一个类型
- 从成员函数指针到另一个类型的强制转换,返回严格的别名问题
- 如何将模板化类型的常量应用到另一个类型