本文介绍了数组下标的无效类型“<未解析的重载函数类型> [int]"-C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将数字保存到向量中时出现此错误...

I've this error when I try to save a number into my vector...

Invalid types ‘<unresolved overloaded function type>[int]’ for array subscript

代码是:

class Elemento{
private:
    int Nodo;

public:
         Elemento(){};
         ~Elemento(){};
    void SetNumero(int x)    {  Nodo = x;  };
    int  GetNumero()         {  return Nodo; };
};


class MagicSquare{
private:
    int    N;
    int    Possibili_N;
    int    Magic_Constant;

    vector<Elemento> Square(int Possibili_N);

public:
    MagicSquare()                   {    };
    ~MagicSquare()                  {    };

    void  Set_N(int x)              { N = x; };
    void  Set_PossibiliN(int x)     { Possibili_N = x; };
    void  Set_MagicConstant(int x)  { Magic_Constant = x; };

    . . .

    void SetSquare(int i, int x)    { Square[i].SetNumero(x); }; // got error here
    int  GetSquare(int i)           { return Square[i].GetNumero(); }; // got error here
};

每当我使用Square [i] .method()...时,我都会出错.

I've got error whenever I use Square[i].method()...

我调用了一个方法,该方法传递Square中的索引和要放入 Elemento-> Nodo 中的值,但是我必须使用公共方法来访问私有的 Nodo.与GET相同.我想获取显示它的值.

I call a method that pass the index in the Square and the value to put in Elemento->Nodo, but I've to use a public method to access to private Nodo. The same with the GET. I want to get the value for displaying it.

推荐答案

您似乎已将 Square 声明为函数,而不是变量.

You seem to have declared Square as a function, not a variable.

相反,声明 vector< Elemento>Square; 并在构造函数中对其进行初始化.

Instead, declare vector<Elemento> Square; and initialize it in the constructor.

这篇关于数组下标的无效类型“&lt;未解析的重载函数类型> [int]"-C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 07:52