【问题标题】:C++ Vectors with pre-determined size throwing compile errors具有预定大小的 C++ 向量引发编译错误
【发布时间】:2015-08-07 07:05:39
【问题描述】:

我对 C++ 非常陌生,并尝试创建一个简单的 Student 类,其中包含 int 类型的分数向量。

这是我的课:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <sstream>

using namespace std;

class Student {
    string last;
    string first;
    vector<int> scores(10);

public:
    Student():last(""), first("") {}
    Student(string l, string f) {
        last = l;
        first = f;
    }
    ~Student() {
        last = "";
        first = "";
    }
    Student(Student& s) {
        last = s.last;
        first = s.first;
        scores = s.scores;
    }
    Student& operator = (Student& s) {
        last = s.last;
        first = s.first;
        scores = s.scores;
        return *this;
    }

    void addScore(int n) {
        scores.push_back(n);
    }
};

由于某种原因,我得到多个 reference to non-static member function must be called; did you mean to call it with no arguments 引用向量 scores

这是我的完整错误列表:

main.cpp:15:22: error: expected parameter declarator
    vector<int> scores(10);
main.cpp:15:22: error: expected ')'
main.cpp:15:21: note: to match this '('
    vector<int> scores(10);
main.cpp:30:4: error: reference to non-static member function must be called; did you mean to call it with no arguments?
            scores = s.scores;
main.cpp:35:4: error: reference to non-static member function must be called; did you mean to call it with no arguments?
            scores = s.scores;
main.cpp:35:15: error: reference to non-static member function must be called; did you mean to call it with no arguments?
            scores = s.scores;
main.cpp:40:4: error: reference to non-static member function must be called; did you mean to call it with no arguments?
            scores.push_back(n);

我尝试了很多方法,但仍然不知道这些错误是从哪里来的。我对 C++ 很陌生,所以请原谅我。任何帮助将不胜感激。

【问题讨论】:

    标签: c++ vector reference compiler-errors non-static


    【解决方案1】:

    你不能像这样初始化一个数据成员:

    vector<int> scores(10);
    

    您需要以下形式之一:

    vector<int> scores = vector<int>(10);
    vector<int> scores = vector<int>{10};
    vector<int> scores{vector<int>(10)};
    

    原因是为了避免看起来像函数声明的初始化。请注意,这在语法上是有效的:

    vector<int>{10};
    

    但它将向量初始化为大小为 1,其中单个元素的值为 10。

    【讨论】:

    • 添加scores = vector(10)会导致以下错误:main.cpp:22:14: error: cannot refer to class template 'vector' without a template argument list scores = vector(10);
    【解决方案2】:

    您不能在类的成员定义中调用构造函数;你需要从初始化列表中调用它:

    class Student {
        string last;
        string first;
        vector<int> scores;
    
    public:
        Student():last(""), first(""), scores(10) {}
    

    编辑至少这是 c++11 之前的方式...

    【讨论】:

    • 你将如何在参数构造函数中初始化scores
    • 以同样的方式:Student(string l, string f) : scores(10) {,但如果可以使用 c++11,请使用 juanchopanza 回答 :)
    【解决方案3】:

    您应该在这种情况下使用初始化列表: Student():scores(10) {}

    如果在代码中下一步使用 push_back(),创建 10 个元素的向量的原因是什么?

    void addScore(int n) {
            scores.push_back(n);
        }
    

    此代码将第 11 个元素添加到向量中。这是您项目的正确行为吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 2020-04-23
      相关资源
      最近更新 更多