试图在类定义中声明数组时出错
Errors trying to declare array in class definition
在过去的一个小时里,我一直试图让这个类拥有一个私有数组数据成员,但它拒绝工作。我不想用array[5]mapArray;因为那样我就不会得到数组成员函数。
这是我目前正在使用的代码。
#include "stdafx.h"
#include <iostream>
#include <array>
#include <fstream>
class Map {
public:
void scanFile();
private:
size_t columns = 20;
size_t rows = 20;
array <int, 5> mapArray;
};
int main() {
Map myMap;
}
下面是我在VisualStudio中遇到的一些错误示例。
1>x:aerofsgtece 2036lab03map.cpp(12): error C2143: syntax error : missing ';' before '<'
1>x:aerofsgtece 2036lab03map.cpp(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>x:aerofsgtece 2036lab03map.cpp(12): error C2238: unexpected token(s) preceding ';'
您出现编译错误。这是因为数组是在命名空间std中定义的。请添加
using namespace std;
在文件顶部,或者在使用其中定义的任何类型之前添加std::
:
std::array< int, 5> mapArray;
后者是首选的,因为您不必仅仅为了使用它的array
类型而从标准库中获取所有符号。
包括std::array
在内的标准STL类是std::
命名空间的一部分。
因此,您可以简单地将std::
名称空间限定符添加到array
数据成员中:
std::array<int, 5> mapArray;
相关文章:
- 在类c++中使用new声明数组
- 为什么不能用常量表达式声明数组?
- 声明数组后我无法添加数组
- 可以使用<size_t>static_const(),std::as_const()或static_cast<const size_t>()来声明数组吗?
- 变量未在此范围内声明 数组线性搜索
- 在类中声明数组. C++
- T D[N] 总是声明数组类型的对象吗?
- 如何使用智能指针在二维上声明数组?
- 声明数组>> int arr[] 时出错;在C++(虽然在 Java 中有效)?
- 在C 类中声明数组数据
- 声明数组时出现 SIGSEGV 错误,而不创建新的 int[size]
- 如何在不初始化常量的情况下声明数组?
- 使用函数在函数中声明数组值
- 以C++声明数组的时间复杂度
- 通过传递数组长度在函数内声明数组
- 通过引用传递数组,而不在函数声明中显式声明数组的大小
- 声明数组和打印[错误?]
- 用内核参数在CUDA内核中声明数组
- 如何在不依赖#Define的情况下转发声明数组大小
- 声明数组已经填充了零