-1

When I try to find the size of an array, Visual Studio returns the error that the identifier doesn't have a class type. I think this might be due to corruption.

#include <iostream>
#include <array>
using namespace std;

int arr[] = {1,2,3};

int main() 
{
   cout << arr.size() << endl;
}

Image of my code

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Have you learnt Java/C# or similar language before coming to C++? Because I doubt any [C++ coursebook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) taught you to use C++ like this. – Yksisarvinen Dec 16 '20 at 17:03
  • 1
    @Yksisarvinen What are you on about? Yes, there are less than desirable practices at play, but it's a two line program. – sweenish Dec 16 '20 at 17:06
  • Arrays are not classes, and do not have functions like size() because they aren't classes. The compiler is right... – user253751 Dec 16 '20 at 17:07
  • 1
    For future reference, if you think an issue with your code is being caused by a bug in the compiler, the issue in your code is not being caused by a bug in the compiler. – Sneftel Dec 16 '20 at 17:07
  • @sweenish I was just referring to OP trying to use `.size()` on array, which would make sense in Java, but absolutely none in C++. I assumed they are trying to learn C++ by guessing and applying their knowledge of Java or C# to C++, which may never work. And I wanted to suggest that they need an actual coursebook. – Yksisarvinen Dec 16 '20 at 17:09
  • 1
    You are aware that the library they included, ``, exists and provides an array type with a `size()` member, right? At the end of the day, this is just a typo. – sweenish Dec 16 '20 at 17:10
  • @Yksisarvinen `array.size()` is a C++ construct, not a Java one. (That would be `array.length`.) – Sneftel Dec 16 '20 at 17:14
  • 1
    @sweenish I included a lot of random headers when I was starting to learn C++, whatever I could find on the internet. Especially when they sounded relevant. I disagree that this is a typo, because it shows a fundamental lack of knowledge of differences between C-style arrays and `std::array` (or classes in general), which should be explained in any C++ coursebook for beginners. But anyway, no point in continuing this discussion, OP already got their answers. – Yksisarvinen Dec 16 '20 at 17:18
  • This example does not include a lot of random headers. OP is not you, and your experiences are not universal. It includes only the necessary headers. That should have been enough to stop your assumption that everyone learns like you did. And honestly, mixing up the two array types makes sense from someone learning a language that has two array types. The assumption that they are inter-changeable makes sense at first glance. Because why would a language ever duplicate itself? My students would constantly mix up C-strings and `std::string` headers for their first few attempts. It's a typo. – sweenish Dec 16 '20 at 17:22

3 Answers3

1

C-style arrays do not have member methods, such as size(). Standard containers, like std::array and std::vector, have size() methods, eg:

#include <iostream>
#include <array>

std::array<int, 3> arr{1,2,3};

int main() 
{
   std::cout << arr.size() << std::endl;
}
#include <iostream>
#include <vector>

std::vector<int> arr{1,2,3};

int main() 
{
   std::cout << arr.size() << std::endl;
}

There is also a std::size() function, which has overloads to work with standard containers, as well as C-style arrays, eg:

#include <iostream>
#include <array>

int arr[] = {1,2,3};

int main() 
{
   std::cout << std::size(arr) << std::endl;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

You declared a C-array, which is not a class, and does not have a size() member.

You wanted to declare a std::array<int, 3> arr{1, 2, 3}; and then you would have access to the size() function.

Link to the relevant page on cppreference.

sweenish
  • 4,793
  • 3
  • 12
  • 23
0

The C array you are using is different from an std::array.

The C++ standard library class std::array has the class member function size.

You are probably looking for something like this :

#include <iostream>
#include <array> 

int main() 
{
   std::array<int,3> numbers {1, 2, 3};
   std::cout << numbers.size() << '\n';
}