“错误:在'a'中请求成员'size',这是指针类型"但我不认为这是一个指针 [英] "error: request for member ‘size’ in ‘a’, which is of pointer type" but I didn't think it was a pointer

查看:119
本文介绍了“错误:在'a'中请求成员'size',这是指针类型"但我不认为这是一个指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想我正在尝试做一些简单的事情,但是显然不是...

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

我编写了此函数,以便稍后进行扩展,并可以通过menu(mystrings)快捷地向用户提供菜单:

I wrote this function so I could extend it later and have a quick way to give the user a menu when required 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 compiling. Could anyone translate that error for me and explain what -> is, thank you.

推荐答案

您正在传递strings数组,并尝试在该数组上调用size().数组在传递给函数时会退化为指针,这说明了您的错误.

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

->运算符或箭头运算符"(我使用的名称)只是(*obj).func()的简写.如果您有一个指向类对象的指针,这将很有用.示例:

The -> operator, or "arrow operator" (name I use), is just 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

这篇关于“错误:在'a'中请求成员'size',这是指针类型"但我不认为这是一个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆