c++ - 尝试通过多个函数移动数组并遇到一个我不知道如何修复的错误

标签 c++

这是错误
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/

相关文章:

C++:按照标准双关字符数组有什么特别之处?

c++ - 在 CMake 中制作自定义中间体(改进 C 可编译测试器)

c++ - 在另一个类内存分配中创建一个类

c++ - 由于 "_fchmodat"在 mac 上使用 boost::filesystem 失败

c++ - 使用 C++ 逐行读取字符串

c++ - gSoap EWS "Error 500: Internal Server Error"

c++ - 直接从已安装的 Windows 光栅(位图)字体获取位图

c++ - SFML loadFromFile 不起作用,奇怪的错误

c++ - 如何实现 MATLAB 与单独的 C++ 应用程序之间的通信?

c++ - OpenCV 与 Qt 集成