长度的左侧必须具有类/结构/nnion

left of .length must have class/struct/nnion

本文关键字:结构 nnion      更新时间:2023-10-16

我是C 的新手,并且整体编程,并且我正在做一个分配,其中我创建了一个类,其中包含forname,姓氏和年龄的变量。这无济于事,但是当我将类向量传递到线性搜索功能时,我的问题就会出现,在该功能中,我会在其中获得"左侧的左侧/结构/结构/联合"的错误。我不知道为什么,在谷歌搜索大约一个小时后,我决定询问。

int linsok(Person p[], int n, int a)
{
  cout << "Please enter the age of the person you want to find: ";
  cin >> a;
  for (n = 0; n < p->Person::Person.end(); n++)
  {
    if ( a = p->Person::Person.setAge)
    {
        cout << "The person you are looking for has the position " << n 
        << " in the list." << endl;
    }
    else
        return -1;
  }}

要澄清,我的任务是创建一个线性搜索,该搜索通过向量进行搜索,以找到与搜索标准相匹配的人的年龄:a,如果没有找到该年龄的人,则返回-1。p>

请注意,p不是std::vector,它只是一个普通的旧数组。n参数应表示其大小,因此:

for (int i = 0; i < n; ++i) {
    // Code...

Person p[]是一个数组。与std::vector<Person>不同,它不能提供通过属性确定其长度的方法。这就是为什么n传递给您的功能的原因,大概是为了指定数组中的元素数量。循环应该看起来像这样:

for (int i = 0; i < n ; i++)

您指的是人的年龄如下:

p[i].age // or getAge(), I don't see you Person's class

注意:范围分辨率::语法更为高级。需要指定要调用哪种函数或在特定名称空间内使用哪种类型。在大多数情况下,您可以暂时忽略它。