Can I use an array as an attribute of struct in C++?

  arrays, attributes, c++, data-structures, struct

Can I define an array of integers as an attribute in a struct in C++?

This is how I tried, output is also posted:

struct SpaceTime{
    SpaceTime(int Coordinates[2][2]):
       Coordinates_(Coordinates[2][2]){}
public:
    int Coordinates_;
};     

int main(){
    SpaceTime earth({{100,200},{300,400}});
    std::cout << earth.Coordinates_[1][1] << std::endl;
}

Output from C++14, Windows 10 Visual Studio:

.StackOverFlow.cpp: In function 'int main()':
.StackOverFlow.cpp:28:39: error: no matching function for call to 'SpaceTime::SpaceTime(<brace-enclosed initializer list>)'
  SpaceTime earth({{100,200},{300,400}});
                                       ^
.StackOverFlow.cpp:22:5: note: candidate: SpaceTime::SpaceTime(int (*)[2])
     SpaceTime(int Coordinates[2][2]):
     ^~~~~~~~~
.StackOverFlow.cpp:22:5: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'int (*)[2]'
.StackOverFlow.cpp:21:8: note: candidate: constexpr SpaceTime::SpaceTime(const SpaceTime&)    
 struct SpaceTime{
        ^~~~~~~~~
.StackOverFlow.cpp:21:8: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const SpaceTime&'
.StackOverFlow.cpp:21:8: note: candidate: constexpr SpaceTime::SpaceTime(SpaceTime&&)
.StackOverFlow.cpp:21:8: note:   no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'SpaceTime&&'
.StackOverFlow.cpp:29:35: error: invalid types 'int[int]' for array subscript
  std::cout << earth.Coordinates_[1][1] << std::endl;

Source: Windows Questions C++

LEAVE A COMMENT