C ++ - бинарный '=': оператор не найден - PullRequest
0 голосов
/ 05 февраля 2020

Я пытаюсь выучить C ++, поэтому я очень новый.

Я пытаюсь написать что-то, что берет вектор и записывает содержимое в файл:

int AsciiConvert::TextToAscii(const std::string file_content, fs::directory_entry entry)
{
    //int test_text_char;

    std::vector<int> temp_text_int;

    std::ofstream output_file(".\\Output\\" + entry.path().filename().string(), std::ios::out);

    std::ostream_iterator<std::string> output_iterator(output_file, "\n");

    for (size_t i = 0; i < file_content.length(); i++)
    {
        temp_text_int.push_back(file_content[i]);
        /*output_file << test_text_char;*/
    }

    //std::cout << temp_text_int.begin() << " " << temp_text_int.end() << std::endl;

    std::copy(temp_text_int.begin(), temp_text_int.end(), output_iterator);

    output_file.close();

    return 0;
}

Однако , когда я компилирую это, я получаю код ошибки C2679 (основанный на строке std :: copy):

C2679   binary '=': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) AsciiConvert    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xutility    3330

Любая помощь будет принята с благодарностью, я чувствую, что я так близок к выполнению этой работы!

1 Ответ

2 голосов
/ 05 февраля 2020

Вы сделали этот материал слишком сложным:

void AsciiConvert::writeCharsCodesToFile(const std::string& text, fs::path path)
{
    std::ostream f{path};
    writeCharsCodes(text, f);
}

std::ostream& AsciiConvert::writeCharsCodes(const std::string& text, std::ostream& out)
{
    for (auto ch : text) {
       out << static_cast<int>(ch) << '\n';
    }
    return out;
}

// or if you prefer stream iterators:
std::ostream& AsciiConvert::writeCharsCodes(const std::string& text, std::ostream& out)
{
    std::copy(text.begin(), text.end(), 
              std::ostream_iterator<int>{out, "\n"});
    return out;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...