Keegs
Keegs

Reputation: 117

Errors trying to declare array in class definition

I've been trying to get this class to have a private array data member for the past hour, but it's refusing to work. I don't want to use array[5] mapArray; because then I don't get the array member functions.

Here is the code I am using at the moment.

#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;
}

And here are some example errors I am getting in Visual Studio.

1>x:\aerofs\gt\ece 2036\lab03\map.cpp(12): error C2143: syntax error : missing ';' before '<'
1>x:\aerofs\gt\ece 2036\lab03\map.cpp(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>x:\aerofs\gt\ece 2036\lab03\map.cpp(12): error C2238: unexpected token(s) preceding ';'

Upvotes: 1

Views: 118

Answers (2)

4pie0
4pie0

Reputation: 29754

You got compilation error. It is because array is defined in namespace std. Either add

using namespace std;

at the top of your file or add std:: before you use any type defined in there:

std::array< int, 5> mapArray;

The latter is preferred because you don't have to bring all symbols from the standard library just to use it's array type.

Upvotes: 1

Mr.C64
Mr.C64

Reputation: 43044

Standard STL classes, including std::array, are part of the std:: namespace.

So, you can simply add the std:: namespace qualifier to your array data member:

std::array<int, 5> mapArray;

Upvotes: 1

Related Questions