使用std::istream引用初始化初始化初始化
Class with std::istream reference initialization
初始化这个类时遇到问题:
class Player{
///
std::istream ∈
///
};
这样尝试:
Player::Player():in(cin){
///
}
有人能为我指明如何实现这一目标的正确方向吗?此外,在初始化之后,我可以通过说之类的话来更改引用吗
stringstream ss("test");
Player p;
p.in = ss;
提前感谢
您没有声明构造函数,只定义了它。
声明构造函数并将其设置为公共:
class Player{
public:
Player(); // You need to declare the constructor
std::istream ∈
};
Player::Player():in(cin)
{}
int main()
{
Player p;
}
我能更改参考号吗?
不,您不能更改引用,只能将值更改为正在引用的值。
相关文章:
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 使用通用值初始化 std::map,不重复
- 初始化 std::vector 替换为单大括号而不是双大括号
- 在构造函数中使用可变参数初始化 std::tuple
- 使用大括号初始化 std::vector 使用初始值设定项列表
- 为什么初始化 std::vector 时不能使用模板整数?
- 如何在构造函数参数中初始化"std::set"?
- 初始化 std::数组 of Eigen::Map
- 我可以列表初始化 std::vector 并完美转发元素吗?
- 在 C++20 之前和之后初始化 std::atomic
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- 如何初始化 std::向量的映射?
- 如何在构造函数初始值设定项列表中使用 n 个元素初始化 std::vector<std::time_t>
- 使用范围库初始化 std::vector
- 如何在类的初始值设定项列表中初始化 std::array,而不对数组的大小进行硬编码
- 我是否需要在构造函数中显式初始化 std::unique_ptr?
- 如何使用派生类类型数据初始化 std::shared_ptr?
- 为什么我必须在初始化 std::array<SomeStruct, size> 时指定每个项目的类型C++
- 根据指针条件初始化std::vector
- 正在使用unique_ptr初始化std::vector