"error: request for member ‘size’ in ‘a’, which is of pointer type"但我不认为这是一个指针
"error: request for member ‘size’ in ‘a’, which is of pointer type" but I didn't think it was a pointer
所以,我以为我想做一些简单的事情,但显然不是。。。
我写这个函数是为了以后可以扩展它,并在需要时通过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
相关文章:
- 不计算一个范围内的完美数
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 选择一个元素而不是一个对象的数组的原因
- 插件:TypeError:addons.function() 不是一个函数
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 只要我不包含一个标题,重新定义C++关键字合法吗
- 你怎么编码,如果x不等于一个数字,程序就会退出
- C++,当函子不是一个选项时,我如何编写带有自定义函数调用的模板化 RAII 包装器?
- 如何让这个C++代码从用户那里读取五个整数而不是一个整数?
- 将字符指针按顺序存储在 map 中 std::map<char*, int> mymap。将其存储为字符或字符串不是一个选项
- makefile不更新一个特定文件
- 为什么即将到来的范围库不支持一个范围的容器初始化
- 为什么我的运算符=(T&&)模板只绑定到一个const&而不是一个&&?
- 两个 4 位位字段加起来不等于一个字节的大小 - 如何修复?
- 为什么 std::list::splice 不是一个免费函数?
- 与不包括一个标头更好的使用不完整的标头比另一个标头的使用无效
- 我们如何有证据表明,声明虚拟函数的类是2个字节,其中一个不超过一个未声明,而在子类中
- 为什么在这种情况下它不是一个 constexpr?
- C 程序处理两个文件,而不是一个文件
- "error: request for member ‘size’ in ‘a’, which is of pointer type"但我不认为这是一个指针