I have a simple question for beginners of c++ Array.

Asked 2 years ago, Updated 2 years ago, 20 views

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

int main() {

    int arrayName[3] = { 1, 2, 3 };
    cout << arrayName[2] << endl;
    cout << "Array size is " << **arrayName**.size() << endl;
    string input_string;
    cout << "Type something: ";
    getline(cin, input_string);
    cout << "Hello, World." << endl;
    cout << "This is what you typed: ";
    cout << input_string << endl;
    input_string.erase(input_string.begin() + 1, input_string.end() - 1);
    cout << input_string;


    return 0;
}

Why is it that the red line is drawn in the arrayName part shown above and the error keeps popping up?

I'd really appreciate it if you could answer me.

c++

2022-09-20 19:16

1 Answers

Because arrayName is a normal array.

For use with a.size(), a must be a classy variable, and there must be a size() function in the member function.

cout << "Array size is " << **arrayName**.size() << endl;

If you want to print the length of the normal array, try changing the code above like below.

cout << "Array size is " << sizeof(arrayName)/sizeof(arrayName[0]) << endl;


2022-09-20 19:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.