左边的[fill in the blank]必须有class / struct / union错误 [英] left of [fill in the blank] must have class/struct/union error

查看:294
本文介绍了左边的[fill in the blank]必须有class / struct / union错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这一行:

ArrayList<Operation> operations();

现在,这是我的ArrayList类和我的操作struct:

Now, this is my ArrayList class and my Operation struct:

typedef struct Operation {
    char key; 
    int value;
} Operation;
template <class DT>
class ArrayList
{
private:
    int _capacity;
    int _size;
    DT* elements;
public:
    ArrayList();
    ~ArrayList();
    void insert (DT&);
    DT& operator[] (int);
};
ArrayList<DT>::ArrayList()
{
    _capacity = 10;
    _size = 0;
    elements = new DT[10];
}



我看不到需要发布其他方法的代码错误不会发生在那里。

I don't see the need of posting the code of the other methods since the error isn't happening there. However if you want to see them you just have to ask.

现在,我每次尝试做

operations.insert(x) //assuming x is a struct that exists.

operations[i].key; //assuming i is a declared and initialized index.

它给我的错误C2228:左边(填空格)必须有类/ struct / union
错误C2109:subscript需要数组或指针类型

我已经看到一个以前的线程关于这个问题,我认为我的问题是编译器采取的第一行代码我提供作为一个声明,但没有初始化。
但是我没有看到这个解决方案。在我的头中,唯一的解决方案是使它成为一个指针,并使用 = new ... 但在我的头中的关键字 new 是肮脏的同义词,所以我只是想让它成为一个对象。什么是绕开这个方法?
或者我错了,这与对象类型是一个 struct 有关,因为这是我第一次使用 struct

I have seen a previous thread about this problem and I think my problem is that the compiler takes the first line of code I provided as a declaration, but no initialization. However I didn't see a solution for this. In my head the only solution is to make it a pointer and use = new ... but in my head the keyword new is synonym of nastiness so I simply wanted to make it a an object. What is a way to go around this? Or perhaps I am wrong, and this has something to do with the object type being a struct since this is the first time I work with structs.

推荐答案

此行:

ArrayList<Operation> operations();

声明一个返回 ArrayList< Operation>

这通常被称为最烦躁的parse

要声明您的 ArrayList< Operation> ,请删除括号:

To declare your ArrayList<Operation>, remove the parenthesis:

ArrayList<Operation> operations;

这篇关于左边的[fill in the blank]必须有class / struct / union错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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