包含类对象的向量在主方法中引发错误

Vector containing class objects throwing errors in main method

本文关键字:错误 方法 对象 向量 包含类      更新时间:2023-10-16

我正在用如下所示的主方法生成Student类型的向量,这是我编写的一个类:

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 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) {

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