【问题标题】:push_back in the vector class in C++C++ 中向量类中的 push_back
【发布时间】:2014-12-18 19:31:51
【问题描述】:

这是我的代码的快照:

int m = 3;
int l = 4;
int k - 6;
std::vector<int> perceptrons;
for(int i = 0; i < k; i++){
    Perceptron Ki = Perceptron(m, l);
    perceptrons.push_back(Ki);
}

我收到以下错误:

Main.cpp:102:33: error: no matching function for call to       ‘std::vector<int>::push_back(Perceptron&)’
     perceptrons.push_back(Ki);

我有一个感知器类,它显然是带有参数 m 和 l 的对象,基本上我只是想创建一个感知器向量。

谁能看出这是为什么?

谢谢

【问题讨论】:

  • 显然这是因为您有一个 vectorints 并且您正尝试向其中添加一个 Perceptron。唯一可行的方法是 Perceptron 类定义了到 int 的隐式类型转换运算符。

标签: c++


【解决方案1】:

因为您试图将Perceptron 放入ints 的向量中。

将您的代码更改为:

int m = 3;
int l = 4;
int k - 6;
std::vector<Perceptron> perceptrons; // This is the line that needs changing
for(int i = 0; i < k; i++){
    Perceptron Ki = Perceptron(m, l);
    perceptrons.push_back(Ki);
}

【讨论】:

  • 我将如何引用感知器中的特定整数,特别是“l”?谢谢。
  • perceptrons[0].l 也许? perceptrons[x] 评估为 Perceptron &amp;,因此您可以像使用任何其他 Perceptron 对象一样使用它。
【解决方案2】:

也许你需要一种不同的向量:

std::vector<Perceptron> perceptrons;

希望那些复制起来很便宜。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-12
    • 2016-11-16
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多