这是错误
hw4.cpp:16:41: 错误:无法将‘std::string {aka std::basic_string}’转换为‘std::string*
{aka std::basic_string}' 用于参数'1'到'std::string randpasswords(std::string)'
writepass(randpasswords(readpasswords()), readnames());
'''代码'''
#include<iostream>
#include<stdlib.h>
#include<string>
#include<fstream>
using namespace std;
string readnames();
string readpasswords();
string randpasswords(string[]);
int writepass(string[], string[][2]);
int main()
{
writepass(randpasswords(readpasswords()), readnames());
return 0;
}
'''函数'''
string readnames()
{
string names[100][2];
ifstream indata;
indata.open("employees.txt");
int x = 0;
while(!indata.eof())
{
indata >> names[x][0];
indata >> names[x][1];
cout << names[x][0] << " " << names[x][1]<< endl;
x = x+1;
}
indata.close();
return names[100][2];
}
string readpasswords()
{
string pass[100];
ifstream indata;
indata.open("passwords.txt");
int x = 0;
while(!indata.eof())
{
indata >> pass[x];
x = x+1;
cout << pass[x] << endl;
}
indata.close();
return pass[100];
}
string randpasswords(string pass[])
{
string randpass[100];
return randpass[100];
}
int writepass(string randpass[], string names[][2])
{
return 0;
}
我想知道为什么在 int main 中函数链不起作用
最佳答案
不要使用像 string[][2]
这样奇怪的类型。对数组使用 std::vector
,对一对 std::string
使用 std::pair
。这是如何声明和实现 readnames()
的示例:
std::vector<std::pair<std::string, std::string>> readnames() {
std::vector<std::pair<std::string, std::string>> names;
std::ifstream indata("employees.txt");
while (true) { // not !indata.eof(), see https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons
std::pair<std::string, std::string> name;
if (!(indata >> name.first >> name.second))
break;
names.push_back(name);
}
return names;
}
其他函数的签名可能是:
std::vector<string> readpasswords();
std::vector<string> randpasswords(const std::vector<string>&);
void writepass(const std::vector<string>&,
const std::vector<std::pair<std::string, std::string>>&);
关于c++ - 尝试通过多个函数移动数组并遇到一个我不知道如何修复的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58963996/