英文:
No members available of 'array' class in C++
问题
我可能遇到了迄今为止最愚蠢的错误。
我正在使用Visual Studio 2022为我的编程期末考试学习,我的数组突然停止工作了。我像这样定义数组,
int arr[10] = {};
但是当我尝试使用数组函数来操作我的数组时,
arr.
在输入'.'后,IntelliSense显示"没有可用的成员",并没有列出任何数组函数。
这是一个bug还是我漏掉了什么东西?
非常感谢。
英文:
I'm probably facing the stupidest error so far.
I was studying for my programming final exam in Visual Studio 2022 and my arrays suddenly stopped working. I define the array like this,
int arr[10] = {};
But when I try to use array functions for my array,
arr.
After putting '.', IntelliSense says "No members available" and no array functions listed.
Is this a bug or am I just missing something here?
Thanks a lot.
答案1
得分: 9
你已经初始化的是C风格数组(正如@UnholySheep已经指出的)。
// C风格数组
// 这分配了一个包含10个int类型元素的C风格数组
int myArray[10];
// C++风格数组
// 包括由libc++提供的数组实现
#include <array>
// 这分配了C++风格数组
std::array<int, 10> myArray;
使用C++风格数组,你将获得像end()
、begin()
等成员函数,但使用C风格数组则没有这些功能。
此外,如果你是新手学习这门语言,我建议你查看cppreference以获取有关语言特性的文档,它非常准确。https://en.cppreference.com/w/cpp/container/array 是std::array的文档链接。
英文:
What you have initialised is C-style array (as @UnholySheep has already pointed out).
// C-style arrays
// This allocates a C-style array of 10 elements of type int
int myArray[10];
// C++ style arrays
// Include the array implementation provided by libc++
#include <array>
// This allocates C++ style array
std::array<int, 10> myArray;
With C++ style arrays you'll get member functions like end()
, begin()
, etc, but not with C-style arrays.
Also if you are new to the language, I would suggest you to look at cppreference for the documentation for the language features, it's quite accurate. https://en.cppreference.com/w/cpp/container/array is the link for documentation for std::array
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论