所以,我以为我正在尝试做一些简单的事情,但显然不是......
我写了这个函数,以便以后可以扩展它,并在需要时通过 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;
}
但出于某种原因我得到:
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();
当我尝试编译时。任何人都可以为我翻译那个错误并解释什么是 ->
吗,谢谢。
最佳答案
您正在传递一个 strings
数组,并尝试对该数组调用 size()
。传递给函数时,数组会退化为指针,这就解释了您的错误。
->
运算符,或“箭头运算符”(我使用的名称),只是 (*obj).func()
的简写。如果您有一个指向类对象的指针,这将很有用。示例:
string *s = &someotherstring;
s->size(); //instead of (*s).size(), saves keystrokes
关于c++ - "error: request for member ‘size’ 中的 ‘a’ 是指针类型 "but I didn' t 认为它是一个指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22921998/