c++ - 如何从 C++ 中的引用类型创建指针变量?
问题描述
我想创建一个全局指针,以便所有函数都可以访问引用。
我尝试将引用地址分配给指针,但是当我尝试访问得到编译错误的值时。
vector<vector<char>>* table;
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
table = &board;
cout << board[0][0] << "\n" << table[0][0];
}
};
Line 70: Char 37: error: invalid operands to binary expression ('basic_ostream<char, std::char_traits<char>>' and '__gnu_cxx::__alloc_traits<std::allocator<std::vector<char, std::allocator<char>>>, std::vector<char, std::allocator<char>>>::value_type' (aka 'std::vector<char, std::allocator<char>>'))
cout << board[0][0] << "\n" << table[0][0];
~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/cstddef:130:5: note: candidate function template not viable: no known conversion from 'basic_ostream<char, std::char_traits<char>>' to 'std::byte' for 1st argument
operator<<(byte __b, _IntegerType __shift) noexcept
^
解决方案
应该是(*table)[0][0]
。
table[0]
等价于(*table)
(vector 的向量board
)。
table[0][x]
只是board[x]
。_
并且vector
不可流式传输。
推荐阅读
- java - 如何在 Java 中实现嵌套的迭代器类
- python - 列表中每个连续段的平均值
- python - 元组递归列表,返回更新版本?
- python - 使用 netmiko 在 cisco 设备中连接 ssh 的问题
- python - 如果数字是 1 位,则预先打印一个空格
- python - Python:for循环中的随机10毫秒延迟
- angularjs - 为什么 AngularJs 不能将我的 {{ }} 识别为表达式?
- r - 在 rstudio 中用 lapply 和 gsub 替换特殊字符
- php - IPinfo 如何使用 file_get_contents 显示 json 数据
- android - 无法解析 intellijIdea 中的符号 R