1

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?

10
  • 3
    missing std:: and #include <array> ? Commented Jul 19, 2018 at 16:09
  • 1
    Did you #include <array>? Commented Jul 19, 2018 at 16:09
  • 6
    array<int, 2> = nums; is also incorrect, you probably want array<int, 2> nums = {42, 42}; Commented Jul 19, 2018 at 16:10
  • 2
    @emsimpson92 because C style arrays are second class citizens Commented Jul 19, 2018 at 16:12
  • 2
    @emsimpson92 Oh yeah and because it doesn't work Commented Jul 19, 2018 at 16:12

2 Answers 2

7

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 }; }
Sign up to request clarification or add additional context in comments.

Comments

1

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
}

4 Comments

If you aren't actually going to initialize anything you can just have return {}; in the function.
@NathanOliver Or just delete the function and leave main empty. Or just delete the program. Or turn off your computer... ;)
@LightnessRacesinOrbit But if I turn off the computer won't the internet get lonely? ;)
@NathanOliver It's okay we can just wake-on-LAN you if we get bored

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.