包含 class 个对象的向量在 main 方法中抛出错误

Vector containing class objects throwing errors in main method

我正在制作一个Student类型的向量,这是我写的class,在如下所示的主要方法中:

int main() {
    ifstream prefile("File.csv");
    string l;
    vector<Student> students;
    while(getline(prefile, l)) {
        istringstream ss(l);
        string token;
        string * lineArr = new string[12];
        int temp = 0;
        while(getline(ss, token, ',')) {
            lineArr[temp] = token;
            temp++;
        }
        Student s(lineArr[0], lineArr[1]);
        for (int i = 2; i < 12; i++) {
            s.addScore(atoi(lineArr[i].c_str()));
        }
        students.push_back(s);
        delete [] lineArr;
    }

    sort(students.begin(), students.end(), compareStudents);

    for (int i = 0; i < students.size(); i++) {
        students.at(i).print();;
    }

    prefile.close();
    return 0;
}

主要方法应该从 File.csv 读取并创建一个学生矢量,每个学生都有名字、姓氏和 10 个分数。这是我的 Student class 供参考:

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

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);
    }

    void print() {
        cout << first << " " << last << ":" << endl;
        cout << scores[0];
        for (int i = 1; i < scores.size(); i++) {
            cout <<  ", " << scores[i];
        }
        cout << endl;
    }
};

出于某种原因,我遇到了无法理解的奇怪错误:

In file included from /Users/.../Desktop/stl/main.cpp:3:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1466:36: error: no matching constructor for initialization of 'Student'
                ::new ((void*)__p) _Tp(__a0);
                                   ^   ~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1582:25: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<Student> >::construct<Student, Student>' requested here
        __alloc_traits::construct(this->__alloc(),
                        ^
/Users/.../Desktop/stl/main.cpp:74:13: note: in instantiation of member function 'std::__1::vector<Student, std::__1::allocator<Student> >::push_back' requested here
                students.push_back(s);
                         ^
/Users/.../Desktop/stl/main.cpp:27:3: note: candidate constructor not viable: 1st argument ('const Student') would lose const qualifier
        Student(Student& s) {
        ^
/Users/.../Desktop/stl/main.cpp:18:3: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
        Student():last(""), first("") {}
        ^
/Users/.../Desktop/stl/main.cpp:19:3: note: candidate constructor not viable: requires 2 arguments, but 1 was provided
        Student(string l, string f) {
        ^
In file included from /Users/.../Desktop/stl/main.cpp:5:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1569:21: error: no matching member function for call to 'construct'
    __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x));
    ~~~~~~~~~~~~~~~~^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1587:9: note: in instantiation of function template specialization 'std::__1::vector<Student, std::__1::allocator<Student> >::__push_back_slow_path<const Student>' requested here
        __push_back_slow_path(__x);
        ^
/Users/.../Desktop/stl/main.cpp:74:13: note: in instantiation of member function 'std::__1::vector<Student, std::__1::allocator<Student> >::push_back' requested here
                students.push_back(s);
                         ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1464:21: note: candidate template ignored: substitution failure [with _Tp = Student, _A0 = Student]
        static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1458:21: note: candidate function template not viable: requires 2 arguments, but 3 were provided
        static void construct(allocator_type& __a, _Tp* __p)
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1470:21: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1477:21: note: candidate function template not viable: requires 5 arguments, but 3 were provided
        static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1535:17: error: no matching function for call to 'construct'
                construct(__a, _VSTD::__to_raw_pointer(__end2-1), _VSTD::move_if_noexcept(*--__end1));
                ^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:874:21: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<Student> >::__construct_backward<Student *>' requested here
    __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_);
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1571:5: note: in instantiation of member function 'std::__1::vector<Student, std::__1::allocator<Student> >::__swap_out_circular_buffer' requested here
    __swap_out_circular_buffer(__v);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1587:9: note: in instantiation of function template specialization 'std::__1::vector<Student, std::__1::allocator<Student> >::__push_back_slow_path<const Student>' requested here
        __push_back_slow_path(__x);
        ^
/Users/.../Desktop/stl/main.cpp:74:13: note: in instantiation of member function 'std::__1::vector<Student, std::__1::allocator<Student> >::push_back' requested here
                students.push_back(s);
                         ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1464:21: note: candidate template ignored: substitution failure [with _Tp = Student, _A0 = Student]
        static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1470:21: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1477:21: note: candidate function template not viable: requires 5 arguments, but 3 were provided
        static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0,
                    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1458:21: note: candidate function template not viable: requires 2 arguments, but 3 were provided
        static void construct(allocator_type& __a, _Tp* __p)
                    ^
3 errors generated.

任何帮助将不胜感激,因为我不知道我做错了什么。

Student(Student& s) {

应该是

Student(const Student& s) {

Student& operator = (Student& s) {

应该是

Student& operator = (const Student& s) {

除非你用常量参数声明你的赋值运算符和复制构造函数,否则其他代码将很难复制你的对象。这正是这里发生的事情,std::vector class 无法复制您的 Student 对象,因此您会遇到编译器错误。