包含类对象的向量在主方法中引发错误
Vector containing class objects throwing errors in main method
我正在用如下所示的主方法生成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
对象,因此会出现编译器错误。
相关文章:
- C++:正在检查LinkedList中的回文-递归方法-错误
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 方法内部但循环仍得到预期的不合格id错误C++
- 重载方法的方式会在使用临时调用时生成编译器错误
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在C++中使用内联方法时出现未定义的符号错误
- 使用类模板的方法链接错误
- 尝试了解在导入的静态方法上使用删除方法时的错误
- 我的模板类方法返回错误类型?
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- C++数组队列实现方法错误
- 方法错误"not all control paths return a value"和方法不返回值
- 为什么我的 setter 方法会产生错误的访问错误
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 为什么调用没有正文的纯虚拟方法不会导致链接器错误?
- Android JNI 未定义对错误 c++ 方法的引用
- 这是哨兵线性搜索的错误方法吗?
- 这是重定向fork和execvp派生的子级的输出的错误方法吗
- flickcurl错误-方法flickr.oauth.access_token失败了
- 错误:方法未在此范围内声明(但已包含)