gpt4 book ai didi

C++ 编译错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:04 25 4
gpt4 key购买 nike

#include<string>
using namespace std;

int main(){
const int SIZE=50;
int count=0;
ifstream fin("phoneData.txt");
ofstream fout("phoneList.txt");
string firstName, lastName, phoneNumber;
if (!fin){
cout<<"Error opening file. program ending."<<endl;
return 0;
}
while (count<SIZE && fin>>phoneNumber[count]){
fin.ignore();
getline (fin, firstName[count], '\n');
fin>>lastName[count];
count++;
}
return 0;

到目前为止,这是我的代码。在我的 while 循环中,getline 有问题,我不断收到这样的错误消息:

error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
1> c:\program files\microsoft visual studio 9.0\vc\include\string(475) : see declaration of 'std::getline'

请帮忙!!!我想不通!

最佳答案

getline (fin, firstName[count], '\n');

应该是:

getline(fin, firstName);

还有更多的问题。这是一种可能的清理方法,它对我无法从您的代码中分辨出来的输入数据做出一些假设:

#include <iostream>
#include <fstream>
#include <string>

int main(){
using namespace std;
ifstream fin("phoneData.txt");
ofstream fout("phoneList.txt");
if (!(fin && fout)){
clog << "Error opening file. program ending.\n";
return 1;
}
const int SIZE=50;
string firstName, lastName, phoneNumber;
for (int count = 0; count < SIZE; ++count) {
getline(fin, phoneNumber, ' ');
getline(fin, firstName, ' ');
getline(fin, lastName);
if (!fin) {
break;
}
fout << lastName << ", " << firstName << " -- " << phoneNumber << '\n';
}
return 0;
}

输入样本:

123 Marcy Darcy
555-0701 Daneal S.

输出样本:

Darcy, Marcy -- 123
S., Daneal -- 555-0701

关于C++ 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1745450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com