C++ C++;
在我的程序中,尝试打印向量的C++ C++;,c++,vector,operator-overloading,C++,Vector,Operator Overloading,在我的程序中,尝试打印向量的end()时遇到错误。代码如下: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <time.h> #include "inFrench.h" using namespace std; class student { int m_
end()
时遇到错误。代码如下:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <time.h>
#include "inFrench.h"
using namespace std;
class student
{
int m_studentNumber;
public:
string nameFirst;
string nameLast;
string nameFull;
int getStudentNumber() { return m_studentNumber; }
void setStudentNumber(int studentNumber) { m_studentNumber = studentNumber; }
};
class group
{
public:
vector<student> groupMembers;
};
ostream& operator<<(ostream& os, const student& s)
{
return os << s.nameFirst << ' ' << s.nameLast;
}
student typeName()
{
student bar;
cout << "Type in a student's first name: ";
cin >> bar.nameFirst;
cout << "Type in that student's last name: ";
cin >> bar.nameLast;
bar.nameFull = bar.nameFirst + " " + bar.nameLast;
return bar;
}
void displayStudents(student listOfStudents[50], int studentHeadCount)
{
for (int i = 0; i < studentHeadCount; i++)
{
cout << listOfStudents[i].nameFull << endl;
cout << listOfStudents[i].getStudentNumber() << endl;
cout << "\n";
}
}
void generateGroups(int numberOfGroups, int maxStudents, int studentsPerGroup, int remainder, group foo, student theStudents[], int studentsBeenAssigned)
{
int k;
numberOfGroups = maxStudents / studentsPerGroup;
cout << numberOfGroups << endl;
srand(time(NULL));
while (studentsBeenAssigned << maxStudents)
{
for (int j = 0; j < maxStudents; j++)
{
k = rand() % maxStudents;
foo.groupMembers.push_back(theStudents[k]);
cout << foo.groupMembers.end() << endl;
studentsBeenAssigned++;
}
}
if (remainder < studentsPerGroup && remainder > 0) // Still coding this section
{
foo.groupMembers.push_back(theStudents[k]);
}
}
void languageChoices()
{
cout << "Select your language from the following:\n";
cout << "a) English\n";
cout << "b) French\n";
cout << "\n";
}
void options()
{
cout << "Select what you want to do:\n";
cout << "1) Exit application\n";
cout << "2) Enter a Student\n";
cout << "3) Display Students\n";
cout << "4) Display Groups\n";
cout << "5) Output groups as text file\n";
cout << "\n";
}
int main()
{
char selectedLanguage;
languageChoices();
cin >> selectedLanguage;
switch (selectedLanguage)
{
case 'a':
{
group finishedListOfStudents;
student allStudents[50]; // Having 50 students alone is ridiculous!
bool endProg = 0;
int maxStudents;
int studentsPerGroup;
int optionSelect;
int studentHeadCount = 0;
int remainder = 0;
int numberOfGroups;
int studentsBeenAssigned;
cout << "GroupPicker 1.0\n";
cout << "Note: This version of the program is intended for purposes of education only, "
<< "specifically for teacher use in a classroom.\n\n";
cout << "How many students are in the class?\n" << "(Note: You cannot have more than 50 in this program)\n";
cin >> maxStudents;
if (maxStudents > 50)
{
cerr << "Too many students!\n" << "Exiting program...\n";
system("PAUSE");
exit(1);
}
if (maxStudents >= 35 && maxStudents <= 50)
{
cout << maxStudents << " students? You are a pro!\n";
}
cout << "How many students per group?\n";
cin >> studentsPerGroup;
if (studentsPerGroup >= maxStudents || studentsPerGroup <= 1)
{
cerr << "You're kidding, right?\n" << "Exiting program...\n";
system("PAUSE");
exit(1);
}
while (endProg == 0) {
options();
cin >> optionSelect;
switch (optionSelect) {
case 1:
endProg = 1;
break;
case 2:
{
if (studentHeadCount == maxStudents)
{
cerr << "You can't enter more than " << maxStudents << " students\n";
}
else
{
allStudents[studentHeadCount] = typeName();
allStudents[studentHeadCount].setStudentNumber(studentHeadCount);
cout << "Student (" << allStudents[studentHeadCount].nameFull << ") entered.\n";
cout << "\n";
studentHeadCount++;
}
break;
}
case 3:
cout << "Current list of students:\n\n";
displayStudents(allStudents, studentHeadCount);
break;
case 4:
{
if (studentHeadCount < studentsPerGroup || studentHeadCount < maxStudents)
{
cerr << "Invalid group parameters.\n" << "Returning to main menu...\n\n";
break;
}
else
{
cout << "Here are the groups:\n";
generateGroups(numberOfGroups, maxStudents, studentsPerGroup, remainder, finishedListOfStudents, allStudents, studentsBeenAssigned);
}
break;
}
case 5:
{
cout << "Saving groups to file...\n";
ofstream studentGroups;
studentGroups.open("studentGroups.txt");
break;
}
}
}
break;
}
case 'b':
{
mainInFrench();
}
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括“INFORENCH.h”
使用名称空间std;
班级学生
{
int m_学生编号;
公众:
字符串名称优先;
字符串nameLast;
字符串nameFull;
int getStudentNumber(){返回m_studentNumber;}
void setStudentNumber(int studentNumber){m_studentNumber=studentNumber;}
};
班级
{
公众:
向量组成员;
};
ostream&operatorstd::vector::end()
返回的不是实际的最后一个元素
您可以这样打印最后一项:
cout << foo.groupMembers.at(foo.groupMembers.size() - 1);
coutstd::vector::end()
返回的不是实际的最后一个元素
您可以这样打印最后一项:
cout << foo.groupMembers.at(foo.groupMembers.size() - 1);
cout结果是,我一开始就不需要向量。我只需要使用数组。我已经回答了我自己的问题。事实证明,我从一开始就不需要向量。我只需要使用数组。我已经回答了我自己的问题。请分享您的“INFORENCH.h”
代码。您不能打印end()
,因为它超过了结束值。您可能需要返回()
。不过,我不认为这是导致编译错误的原因。@herohuyongtao它与main.cpp相同,但文本是法语的。还请共享您的“INFORENCH.h”
。您不能打印end()
,因为它是超过结束值的代码。您可能需要返回()
。我不认为这是导致编译错误的原因。@herohuyongtao它与main.cpp相同,但文本是法语的。或者foo.groupMembers.back()
。或者foo.groupMembers[foo.groupMembers.size()-1)]
。但是向量不能像数组那样定位值吗?@DJHead-On:no。查看std::vector
上的文档,尤其是at
和operator[]
。是一个很好的参考。或者foo.groupMembers.back()
。或者foo.groupMembers[foo.groupMembers.size()-1)]
。但是向量不是不能像数组那样定位值吗?@DJHead-On:no。查看std::vector
上的文档,尤其是at
和operator[]
。是一个很好的参考。使用向量通常意味着以后疼痛会减轻。它们负责内存管理,跟踪大小,可以确保您不会读/写越界等。如果您使用普通阵列,所有这些都必须手动完成std::vector
通常是处理连续同质数据集的更好选择。@Fredrarson可能是这样,但就我使用它们的目的而言,它们是完美的。@DJHead On:一旦您对vector
更加熟悉,我想您会发现它是一种更强大、更易于使用的数组。当然,这取决于您的预期用途,但在大多数情况下,我会在基本数组上选择vector
。@DJHead-on:您迟早会学到其他的方法。他们在这里并不完美。例如,您的班级人数限制为50人。您正在分配一个50的数组,即使类的大小要小得多。向量的大小可以精确。如果你有一个有200或更多学生的礼堂式的课堂呢?我在大学里有好几次这样的经历。数组是C++中非常低级的概念,充满了危险。请注意,我之前的评论是一个链接。阅读链接。@Fredrarson这是真的,但就像我在程序中写的:cout使用向量通常意味着以后更少的痛苦。它们负责内存管理,跟踪大小,可以确保您不会读/写越界等。如果您使用普通阵列,所有这些都必须手动完成std::vector
通常是处理连续同质数据集的更好选择。@Fredrarson可能是这样,但就我使用它们的目的而言,它们是完美的。@DJHead On:一旦您对vector
更加熟悉,我想您会发现它是一种更强大、更易于使用的数组。当然,这取决于您的预期用途,但在大多数情况下,我会在基本数组上选择vector
。@DJHead-on:您迟早会学到其他的方法。他们在这里并不完美。例如,您的班级人数限制为50人。您正在分配一个50的数组,即使类的大小要小得多。向量的大小可以精确。如果你有一个有200或更多学生的礼堂式的课堂呢?我在大学里有好几次这样的经历。数组是C++中非常低级的概念,充满了危险。请注意,我之前的评论是一个链接。阅读链接。@Fredrarson这是真的,但就像我在程序中写的:cout