"error: query for the size of a member in 'a that has a pointer type", but I did not think it was a pointer

So, I thought I was trying to do something simple, but apparently not ...

I wrote this function in order to expand it later and have a quick way to provide the user with a menu when needed by going menu(mystrings) :

 int menu(string a[]) { int choice(0); cout << "Make a selection" << endl; for(int i=0; i<a.size(); i++) { cout << i << ") " << a[i] << endl; } cin >> choice; cout << endl; return choice; } 

But for some reason I get:

 main.cpp: In function 'int menu(std::string*)': main.cpp:38:12: error: request for member 'size' in 'a', which is of pointer type 'std::string* {aka std::basic_string<char>*}' (maybe you meant to use '->' ?) int n = a.size(); 

when I try to compile. Can anyone translate this error for me and explain that -> , thanks.

+7
c ++ arrays
source share
1 answer

You are passing an array of strings and trying to call size() on the array. Arrays degenerate to pointers when passing the function, which explains your error.

The operator -> or the "arrow operator" (the name I use) is just a shorthand for (*obj).func() . This is useful if you have a pointer to a class object. Example:

 string *s = &someotherstring; s->size(); //instead of (*s).size(), saves keystrokes 
+10
source share

All Articles