c++ - Vector containing class objects throwing errors in main method -
i'm making vector of type student
, class have written, in main method shown below:
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]); (int = 2; < 12; i++) { s.addscore(atoi(linearr[i].c_str())); } students.push_back(s); delete [] linearr; } sort(students.begin(), students.end(), comparestudents); (int = 0; < students.size(); i++) { students.at(i).print();; } prefile.close(); return 0; }
the main method supposed read file.csv
, create vector of students each have first name, last name, , 10 scores. here student
class reference:
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]; (int = 1; < scores.size(); i++) { cout << ", " << scores[i]; } cout << endl; } };
for reason, getting weird errors cannot comprehend:
in file included /users/.../desktop/stl/main.cpp:3: in file included /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38: in file included /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../include/c++/v1/ios:216: in file included /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15: in file included /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../include/c++/v1/string:439: in file included /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 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') lose const qualifier student(student& s) { ^ /users/.../desktop/stl/main.cpp:18:3: note: candidate constructor not viable: requires 0 arguments, 1 provided student():last(""), first("") {} ^ /users/.../desktop/stl/main.cpp:19:3: note: candidate constructor not viable: requires 2 arguments, 1 provided student(string l, string f) { ^ in file included /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 call '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, 3 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, 3 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, 3 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 call '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, 3 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, 3 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, 3 provided static void construct(allocator_type& __a, _tp* __p) ^ 3 errors generated.
any appreciated because have no idea i'm doing wrong.
student(student& s) {
should be
student(const student& s) {
and
student& operator = (student& s) {
should be
student& operator = (const student& s) {
unless declare assignment operator , copy constructor const arguments other code going have hard time copying objects. that's happening here, std::vector
class cannot copy student
object, compiler errors.
Comments
Post a Comment