c++中排序函数错误
C++ error in sort function
我正在为一个作业做一个学生记录系统。当尝试运行程序时,我得到错误:
"请求从'std::stringstream'转换为非标量类型"
它来自stringstream key = student[j].getLastName
线。不知道哪里出了问题。
#include <sstream>
#include <iostream>
#include <fstream>
#include "Date.h"
#include "Address.h"
#include "student.h"
using namespace std;
int main(){
string line;
ifstream inputFile("studentdata.txt");
bool keepGoing = true;
Student *student = new Student[50];
int i = 0;
while(!inputFile.eof()){
getline(inputFile, line);
student[i].setInfo(line);
i++;
}
int choice;
cout << "A Heap of Students";
while(keepGoing){ //This while loop creates the menu and asks for user input
cout << "What would you like to do?" << endl;
cout << "1. Print full report." << endl;
cout << "2. Print simple report." << endl;
cout << "3. Sort records." << endl;
cout << "4. Quit" << endl;
cin >> choice;
//prints full student report
if(choice == 1){
for(int i = 0; i < 50; i++){ //Full print loop
cout << student[i] << endl;
}
cout << endl;
keepGoing = true;
}
//Just first and last name
else if(choice == 2){
cout << "First Last" << endl;
for(int i = 0; i < 50; i++){ //Simple print loop
cout << student[i].getFirstName() << " " << student[i].getLastName() << endl;
}
cout << endl; //formatting
keepGoing = true;
}
//sort function
else if(choice == 3){
for(int j = 1; j < 50; j++){
stringstream key = student[j].getLastName;
int i = j - 1;
while(i > 0 && student[i].getLastName > key){
student[i+1] = student[i];
i = i - 1;
}
Student[i + 1] = key;
}
for j = 1 to A.length
key = A[j]
i = j - 1
while i > 0 and A[i] > key
A[i + 1] = A[i]
i = i - 1
A[i + 1] = key
keepGoing = true;
}
//quit
else if(choice == 4){
cout << "Goodbye!" << endl;
keepGoing = false;
}
}
return 0;
}
当您执行stringstream key = student[j].getLastName
时,您尝试从getLastName
复制创建一个新的stringstream
对象。现在,getLastName
可能是一个成员函数,所以编译器不知道如何从它构建stringstream
。
您可能希望用成员函数返回的值初始化stringstream
,因此:
stringstream key(student[j].getLastName());
相关文章:
- 链接器错误:函数的多个定义
- 编译器错误:函数调用在常量表达式中必须有一个常量值
- 错误:函数声明符之后的预期函数体
- C 错误 - 函数不能超载
- 如何修复传递参数时调用错误函数的主函数?(C++)
- 错误:函数调用中有两个参数
- 错误:函数不是“类”的静态数据成员 - C++
- C ,G 编译错误函数
- CMake 解析错误函数缺少结尾")"。而是找到带有文本的未终止字符串")
- C++ 错误 函数 2 的多重定义
- 错误:函数调用中从int到int(*)[4]的转换无效
- C++14 自动扣除错误:函数返回一个数组
- 视觉C++错误:函数必须返回一个值
- 错误:函数声明中的两种或多种数据类型
- 错误:函数未在作用域中声明
- rtw_android.c错误:函数“strnicmp”的隐式声明[-Weror=隐式函数声明]
- 错误:函数中的return语句没有值,返回“void*”[-fpermission]
- 简单程序中的链接器错误:函数的多重定义
- 错误:函数参数太多
- 错误:函数__tmaincrtstartup中引用的未解析的外部符号_main