对成员.. in ..的错误请求,该请求是非类型-C++

发布于 2025-01-31 09:17:39 字数 1212 浏览 2 评论 0原文

我有一个错误:“错误:'tablou [j]''的nume'请求,该'tablou [j]'是非类型'[100]''的请求,我真的不知道如何解决。尝试在YouTube和Google上搜索,但我什么也没发现。有人对如何解决这个问题有任何想法吗?

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

struct{
    int counter;
    char nume[20] = " ";
}tablou[10][100];

int main()
{
    int n, counter = 0;
    char second[10][100];
    bool verify = true;

    cout<<"Cate nume?";
    cin>>n;

    for(int i = 0; i <= n; i++)
    {
        cin.getline(second[i],20);
    }

    for(int i = 0; i <= n; i++)
    {
        verify = true;

        for(int j = 0; j < i; j++)
        {
            if(strcmp(second[i], tablou[j].nume) == 0)
            {
                verify = false;
            }
        }

        if(verify == true)
        {
            strcpy(tablou[i].nume, second[i]);

            for(int k = 0; k < n; k++)
            {
                if(strcmp(tablou[i].nume, second[k]))
                {
                    tablou[i].counter++;
                }
            }
        }
    }

    for(int i = 0; i <= n; i++)
    {
        cout<<tablou[i].nume<<" "<<tablou[i].counter<<endl;
    }

    return 0;
}

I have got this error: "error: request for member 'nume' in 'tablou[j]', which is of non-class type ' [100]'", and I don't really know how to solve it.I tried searching on youtube and google but I found nothing .Does anyone have any ideas for how to solve this?

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

struct{
    int counter;
    char nume[20] = " ";
}tablou[10][100];

int main()
{
    int n, counter = 0;
    char second[10][100];
    bool verify = true;

    cout<<"Cate nume?";
    cin>>n;

    for(int i = 0; i <= n; i++)
    {
        cin.getline(second[i],20);
    }

    for(int i = 0; i <= n; i++)
    {
        verify = true;

        for(int j = 0; j < i; j++)
        {
            if(strcmp(second[i], tablou[j].nume) == 0)
            {
                verify = false;
            }
        }

        if(verify == true)
        {
            strcpy(tablou[i].nume, second[i]);

            for(int k = 0; k < n; k++)
            {
                if(strcmp(tablou[i].nume, second[k]))
                {
                    tablou[i].counter++;
                }
            }
        }
    }

    for(int i = 0; i <= n; i++)
    {
        cout<<tablou[i].nume<<" "<<tablou[i].counter<<endl;
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿哥们儿 2025-02-07 09:17:39

tablou是一个2D数组,

struct{
    int counter;
    char nume[20] = " ";
}tablou[10][100];

其元素是tablou [x] [y]

您尝试访问只有一个索引的元素

if(strcmp(second[i], tablou[j].nume) == 0)
----------------------------^

,我不知道您的代码要做什么错误

tablou is a 2d array

struct{
    int counter;
    char nume[20] = " ";
}tablou[10][100];

its elements are tablou[x][y]

you try to access an element with only one index

if(strcmp(second[i], tablou[j].nume) == 0)
----------------------------^

I do not know what your code is trying to do , but thats why you get the error

陈独秀 2025-02-07 09:17:39

数组tableou是一个二维的数组

struct{
    int counter;
    char nume[20] = " ";
}tablou[10][100];

,例如表达式tablou [j]具有数组类型the_unnamed_structure [100]。

因此,这样的表达

tablou[j].nume

不正确,并且编译器会发出错误。

以下声明。

struct{
    int counter;
    char nume[20] = " ";
}tablou[10];

也许实际上,您的意思是在这些循环中,也

for(int i = 0; i <= n; i++)
{
    verify = true;

    for(int j = 0; j < i; j++)
    {
        if(strcmp(second[i], tablou[j].nume) == 0)
        {
            verify = false;
        }
    }

    if(verify == true)
    {
        strcpy(tablou[i].nume, second[i]);

        for(int k = 0; k < n; k++)
        {
            if(strcmp(tablou[i].nume, second[k]))
            {
                tablou[i].counter++;
            }
        }
    }
}

可以在数组的某些元素tablou中进行 因为您使用索引i来分配数组tablou的元素。那就是数组tableou的实际元素数量可以小于n。在这种情况下,对于循环,这

for(int i = 0; i <= n; i++)
{
    cout<<tablou[i].nume<<" "<<tablou[i].counter<<endl;
}

将调用未定义的行为,因为数据成员counter对于数组的某些输出元素将不进行原始化。

您需要支持一个单独的变量作为数组tablou中的索引。

The array tablou is a two-dimensopnal array

struct{
    int counter;
    char nume[20] = " ";
}tablou[10][100];

So for example the expression tablou[j] has the array type the_unnamed_structure[100].

So such an expression like this

tablou[j].nume

is incorrect and the compiler issues an error.

Maybe actually you mean the following declaration of the array

struct{
    int counter;
    char nume[20] = " ";
}tablou[10];

Also in these loops

for(int i = 0; i <= n; i++)
{
    verify = true;

    for(int j = 0; j < i; j++)
    {
        if(strcmp(second[i], tablou[j].nume) == 0)
        {
            verify = false;
        }
    }

    if(verify == true)
    {
        strcpy(tablou[i].nume, second[i]);

        for(int k = 0; k < n; k++)
        {
            if(strcmp(tablou[i].nume, second[k]))
            {
                tablou[i].counter++;
            }
        }
    }
}

some elements of the array tablou can be skipped if verify is set to false because you are using the index i to assign elements of the array tablou. That is the number of actual elements of the array tablou can be less than n. In this case this for loop

for(int i = 0; i <= n; i++)
{
    cout<<tablou[i].nume<<" "<<tablou[i].counter<<endl;
}

will invoke undefined behavior because the data member counter will be uninitialized for some outputted elements of the array.

You need to support a separate variable as an index in the array tablou.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文