2014-02-23 1 views
0

내 프로그램에서 벡터의 end()을 인쇄하는 중 오류가 발생했습니다. 나는 다음과 같은 결과를 얻을 수CUT 연산자 문제가있는 벡터

#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(); 
     } 
    } 
} 

: 여기에 코드입니다

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Myvec>' (or there is no acceptable conversion) [Line 66] 
IntelliSense: no operator "<<" matches these operands. operand types are: std::ostream << std::_Vector_iterator<std::_Vector_val<std::_Simple_types<student>>> [Line 66] 

내가 다시 ostream operator<< 연산자를 오버로딩 생각했다,하지만 또 다른 옵션이있다?

+0

또한 "inFrench.h"의 코드를 공유하십시오. – herohuyongtao

+0

'end()'를 인쇄 할 수 없습니다. 단 하나의 과거 값이기 때문입니다. 아마'back()'을 원할 것이다. 나는 그것이 당신의 컴파일 오류를 일으키는 것으로 생각하지 않는다. –

+0

@herohuyongtao main.cpp와 동일하지만, 텍스트는 프랑스어로되어 있습니다. –

답변

4

std::vector::end()은 실제 마지막 요소가 아니라 an iterator to the end of the vector을 반환합니다.

당신이 대신 같은 마지막 항목을 인쇄 할 수 있습니다 :

cout << foo.groupMembers.at(foo.groupMembers.size() - 1); 
+6

또는'foo.groupMembers.back()'. –

+0

또는'foo.groupMembers [foo.groupMembers.size() - 1)]'. – herohuyongtao

+0

그러나 배열이 할 수있는 방식으로 벡터를 찾을 수 없습니까? –

0

밝혀, 나는 처음에 벡터를 필요로하지 않습니다. 방금 배열을 사용해야했습니다. 나는 내 자신의 질문에 대답했다.

+2

[배열은 악합니다.] –

+0

벡터를 사용하면 보통 나중에 통증이 줄어 듭니다. 그들은 메모리 관리를 돌보고, 크기를 추적하며, out-of-bounds를 읽거나 쓰지 않도록 할 수 있습니다. 일반적인 배열을 사용한다면이 모든 작업을 수동으로 수행해야합니다. 'std :: vector'는 연속적인 일련의 균질 데이터를 다루는 데 더 좋은 선택입니다. – dreamlax

+0

@FredLarson 그럴 수도 있습니다.하지만 제가 사용하고있는 것에 대해서는 완벽합니다. –