【发布时间】:2015-05-30 16:33:22
【问题描述】:
我正在尝试访问 std::array 的元素,因为它在 C++ 中给出了它的指针。这是一些说明我的问题的代码:
#include <iostream>
#include <array>
void func(std::array<int, 4> *);
int main()
{
std::array<int, 4> arr = {1, 0, 5, 0};
func(&arr);
}
void func(std::array<int, 4> *elements)
{
for (int i = 0; i < 4; i = i + 1)
{
std::cout << *elements[i] << std::endl;
}
}
我希望这会在新行上打印std::array 的每个元素。但是,它甚至没有通过编译:
main.cpp: In function ‘void func(std::array<int, 4ul>*)’:
main.cpp:16:22: error: no match for ‘operator*’ (operand type is ‘std::array<int, 4ul>’)
std::cout << *elements[i] << std::endl;
这是怎么回事?
谢谢!
【问题讨论】:
-
语法很笨拙,这是使用 & 引用而不是 * 指针传递数组的好理由。反正你也不喜欢 nullptr。
标签: c++ function pointers compiler-errors stdarray