Alex Feinstein
Alex Feinstein

Reputation: 391

Using array class to return array c++

I have this function:

array<int, 2> foo() { array<int, 2> = nums; return nums;}

This returns the error "array does not name a type". Why is this?

Upvotes: 1

Views: 96

Answers (2)

Alex Johnson
Alex Johnson

Reputation: 958

You need to include array. And as was pointed out, you have the incorrect syntax for the array declaration.

Try this:

#include <array>

std::array<int, 2> foo() { 
  std::array<int, 2> nums; 
  return nums;
}

int main() {
  // use your function here
}

Upvotes: 1

Caleth
Caleth

Reputation: 62636

The template is spelled std::array, not array, and requires you to #include <array> somewhere preceding that line

#include <array>

std::array<int, 2> foo() { return { 42, 42 }; }

Upvotes: 7

Related Questions