C++ C++;Visual Studio 2017使用对象的my Vector获取错误代码C3867
我现在陷入了困境。我试图将对象向量传递到我的函数中,该函数将打印或保存到文件中。但每当我试着循环我的向量时,它会说“C3867”C++ C++;Visual Studio 2017使用对象的my Vector获取错误代码C3867,c++,c++11,object,vector,c++14,C++,C++11,Object,Vector,C++14,我现在陷入了困境。我试图将对象向量传递到我的函数中,该函数将打印或保存到文件中。但每当我试着循环我的向量时,它会说“C3867” 我的代码是: #include <iostream> #include <string> #include <vector> #include "Student.h" using namespace std; /* FUNCTIONS */ void programStart(); void saveClassInfo(ve
我的代码是:
#include <iostream>
#include <string>
#include <vector>
#include "Student.h"
using namespace std;
/* FUNCTIONS */
void programStart();
void saveClassInfo(vector<Student> &newClass);
void printClassInfo(vector<Student> &newClass);
int main()
{
programStart();
cout << "Press any key to close...";
cin.get();
cin.get();
return EXIT_SUCCESS;
}
void programStart()
{
int size_of_class;
vector<Student> my_class;
cout << "What is the size of the class: ";
cin >> size_of_class;
for (int i = 0; i < size_of_class; i++) {
string student_name;
char student_grade;
cout << "Name of student " << (i + 1) << ": ";
cin >> student_name;
cout << "Grade of student " << (i + 1) << ": ";
cin >> student_grade;
if (student_grade >= 'A' || student_grade <= 'F') {
Student newStudent(student_name, student_grade);
my_class.push_back(newStudent);
}
else {
while (student_grade < 'A' || student_grade > 'F') {
cout << "Input the correct grade: ";
cin >> student_grade;
}
Student newStudent(student_name, student_grade);
my_class.push_back(newStudent);
}
}
//saveClassInfo(my_class);
printClassInfo(my_class);
}
void saveClassInfo(vector<Student> &newClass)
{
ofstream newFile;
newFile.open("class_info.txt");
if (newFile.fail()) {
perror("The following error occured");
}
else {
for (int i = 0; i < newClass.size(); i++) {
newFile << newClass[i].getName << "\t\t"
<< newClass[i].getGrade << endl;
}
}
newFile.close();
cout << "Done saving.\n";
}
void printClassInfo(vector<Student> &newClass)
{
for (int i = 0; i < newClass.size(); i++) {
cout << newClass[i].getName << "\t\t" << newClass[i].getGrade << endl;
}
}
有人能告诉我我的代码有什么问题吗?在:
newFile << newClass[i].getName << "\t\t"
newFilegetName
是一个函数,你忘了括号,我真傻。。。现在,在我添加了括号之后,它开始工作了!非常感谢你!我犯了一个愚蠢的错误。。。这表明我在编程方面缺乏经验。谢谢!现在修好了!
#include "Student.h"
Student::Student(string student_name, char student_grade)
{
setName(student_name);
setGrade(student_grade);
}
string Student::getName()
{
return _name;
}
char Student::getGrade()
{
return _grade;
}
void Student::setName(string student_name)
{
_name = student_name;
}
void Student::setGrade(char student_grade)
{
_grade = student_grade;
}
newFile << newClass[i].getName << "\t\t"