【发布时间】:2015-09-14 03:30:40
【问题描述】:
所以这是我的错误程序:
// 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。我试图在不包含 * 符号的情况下运行程序,但这会显示每个变量的地址并导致崩溃。我该如何解决?谢谢。
【问题讨论】:
-
根据msdn.microsoft.com/en-us/library/bzf3eha6.aspx,c2100 的意思是“间接运算符 ( * ) 应用于非指针值。”很好的暗示你不想要*。