2015-09-14 122 views
-4

因此,这里是我的程序出现错误:用迭代器解决c2100问题?

// ConsoleApplication42.cpp : Defines the entry point for the console application. 
// 

    #include "stdafx.h" 
    #include <iostream> 
    #include <list> 
    #include <cstdlib> 
    #include <time.h> 
    #include <algorithm> 
    #include <string> 
    #include <vector> 

    using namespace std; 


    const int numberOfStudents = 9; 
    string names[numberOfStudents] = {"Abe","Billy","Carl","Dillan","Eddie","Felix","Gill","Herald","Isaac"}; 

    struct StudentInfo { 
     string name; 
     int grade; 



     bool operator< (int grade){ 
      return grade < grade; 
     } 

     bool operator< (string name){ 
      return name < name; 
     } 
     }; 



    void populateStudentRecords(vector<StudentInfo>Students,vector<int>::iterator iter, int x){ 

     for(auto iter = Students.begin(); iter != Students.end(); ++iter){ 
      iter->name = names[x]; 
      iter->name.push_back(x); 
      iter->grade = x++; 
      x = x++; 

     } 



    } 

    bool sortByName(const StudentInfo x, const StudentInfo y){ 
     return x.name < y.name; 
    } 

    bool sortByGrade(const StudentInfo x, const StudentInfo y){ 
     return x.grade < y.grade; 
    } 

    void displayRecords(vector<StudentInfo>Records,vector<int>::iterator iter){ 
     for(auto iter = Records.begin(); iter != Records.end(); ++iter){ 
      cout<<*iter->name<<" -"<<*iter->grade<<endl; 
     } 
    } 

    void displayMaxAndMinGrade(vector<StudentInfo>Records,vector<int>::iterator iter){ 
     for(auto iter = Records.begin(); iter != Records.end(); ++iter){ 
      cout<<*iter->name<<" - " <<*iter->grade<<endl; 
      iter = Records.end(); 
      cout<<*iter->name<<" - " <<*iter->grade<<endl; 
     } 
    } 

    int _tmain(int argc, _TCHAR* argv[]) 
    { 
     vector<StudentInfo>Records (numberOfStudents); 
     vector<int>::iterator iter; 


     populateStudentRecords(Records,iter,0); 
     sort(Records.begin(),Records.end(),sortByName); 
     displayRecords(Records,iter); 
     sort(Records.begin(),Records.end(),sortByGrade); 
     cout<<" "<<endl; 
     displayMaxAndMinGrade(Records, iter); 

     return 0; 
    } 

在displayRecords功能和displayMaxAndMin功能,我旁边的迭代器的*符号。我希望计算机在向量中每次出现结构时显示这些变量的值。但是,当我尝试构建程序时,出现错误c2100。我试图运行该程序时不包含*符号,但显示每个变量的地址并导致崩溃。我该如何解决?谢谢。

+0

据到https:/ /msdn.microsoft.com/en-us/library/bzf3eha6.aspx,c2100表示​​“间接运算符(*)应用于非指针值。”非常好的暗示,你不想*。 – user4581301

回答

1

您需要阅读关于使用C++进行编程的介绍性书籍。这段代码充满了错误。你不知道如何使用指针/引用/迭代器。

  1. 了解一本好书的复制,参考和指针。请按照本答案末尾的链接。
  2. 变量应限制在它们的使用范围内。当他们不需要他们的时候,不要把这么多的参数传递给你的函数。
  3. 当您只需要一个适用于运算符的类型时,您正在同时使用间接引用运算符和取消引用运算符。这是你错误的原因。
  4. 您正在为每个循环增加一个数组索引两次,这使得它超出边界。
  5. 您正在使用容器的end()成员函数,它假定它将一个迭代器返回到容器中的最后一个元素。
  6. 您想要打印最低和最高的等级,但是会遍历整个容器。

为你着想,我已经解决了最明显的错误:

#include <iostream> 
#include <list> 
#include <cstdlib> 
#include <time.h> 
#include <algorithm> 
#include <string> 
#include <vector> 

using namespace std; 

const int numberOfStudents = 9; 
string names[numberOfStudents] = { "Billy", "Abe","Carl","Dillan","Eddie","Felix","Gill","Herald","Isaac" }; 

struct StudentInfo 
{ 
    string name; 
    int grade; 
}; 

void populateStudentRecords(vector<StudentInfo>& Students) 
{ 
    int x{ 0 }; 
    for (auto iter = Students.begin(); iter != Students.end(); ++iter) 
    { 
     iter->name = names[x]; 
     iter->name.push_back(x); 
     iter->grade = ++x; 
    } 
} 

bool sortByName(const StudentInfo x, const StudentInfo y) 
{ 
    return x.name < y.name; 
} 

bool sortByGrade(const StudentInfo x, const StudentInfo y) 
{ 
    return x.grade < y.grade; 
} 

void displayRecords(vector<StudentInfo>Records) 
{ 
    for (auto iter = Records.begin(); iter != Records.end(); ++iter) 
    { 
     cout << iter->name << " -" << iter->grade << endl; 
    } 
} 

void displayMaxAndMinGrade(vector<StudentInfo>Records) 
{ 
    auto iter = Records.begin(); 
    cout << iter->name << " - " << iter->grade << endl; 
    iter = Records.end() - 1; 
    cout << iter->name << " - " << iter->grade << endl; 
} 

int main() 
{ 
    vector<StudentInfo>Records(numberOfStudents); 

    populateStudentRecords(Records); 
    sort(Records.begin(), Records.end(), sortByName); 
    displayRecords(Records); 
    sort(Records.begin(), Records.end(), sortByGrade); 
    cout << " " << endl; 
    displayMaxAndMinGrade(Records); 

    return 0; 
} 

请帮自己一个忙,请访问以下链接:

The Definitive C++ Book Guide and List