【问题标题】:Constructor: function does not take 3 arguments构造函数:函数不接受 3 个参数
【发布时间】:2016-03-25 19:32:19
【问题描述】:

为什么"nota(sinais, subs, indices);" 行告诉函数不接受 3 个参数。我用 3 定义了一个构造函数。

class Solucao{
        bool    *sinal;
        bool    *sublinhado;
        int     *indice;
public:
        Solucao(){sinal = sublinhado = NULL;  indice = NULL; };
        Solucao (bool *sinais, bool *subs, int *indices)
        {
            sinal = sinais;
            sublinhado = subs;
                indice = indices;
        };
};


void Balas(int n, int m, Vector<float> c, Vector<float> b, Matrix<float> A) {
No_Balas *J = NULL;
Solucao *nota();
bool *sinais = new bool[1];
bool *subs = new bool[1];
int *indices = new int[1];

Vector<int> pto_inicial(1);

    pto_inicial[0] = 0; 
    sinais[0] = 0;
    subs[0] = 0;
    indices[0] = 0;

nota(sinais, subs, indices);
}

【问题讨论】:

  • 在您声明对象的行中似乎不正确。
  • 这是一个指针,使用new 就像:nota = new Solucao(sinais, subs, indices); 而不是nota(sinais, subs, indices);

标签: c++ class oop constructor


【解决方案1】:

nota 实例有一个 Solucao* 类型(即,它是一个指针——我假设额外的括号是一个错字,并且您没有尝试声明一个函数)而不是 Sulucao 类型.

根据您当前的代码,您似乎正在尝试执行以下操作:

nota = new Solucao(sinais, subs, indices);

但是,除非您有充分的理由这样做,否则我可能会建议您不要使用 new。相反,您可以删除 Solucao *nota(); 并在拥有所有必需参数后构建它:

Solucao nota(sinais, subs, indices);

注意:如果您继续使用动态分配(即new),我建议您使用符合 C++11 的编译器并了解可用的智能指针。例如:

std::unique_ptr<Solucao> nota = std::make_unique(sinais, subs, indices);

【讨论】:

    【解决方案2】:
    Solucao *nota();
    

    这声明了一个名为nota 的函数,它不接受任何参数并返回一个指向Solucao 的指针。所以编译器是正确的,它不需要 3 个参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 2016-09-03
      • 2014-02-09
      • 2011-01-10
      • 2019-05-27
      • 2013-01-27
      • 1970-01-01
      相关资源
      最近更新 更多