编译错误的翻译

时间:2014-04-25 00:01:38

标签: c++

我收到这些错误,我不明白为什么,程序在学校编好而不是它的疯狂,任何人都可以翻译吗?

functions.cpp: In function 'void printCreatures(Creatures*, int)':
functions.cpp:248:35: error: no matching function for call to 'std::basic_fstrea
m<char>::open(std::string&, const openmode&)'
functions.cpp:248:35: note: candidate is:
In file included from Prog4.h:13:0,
                 from functions.cpp:2:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/fstream:871:7: note: void std:
:basic_fstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [wit
h _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std
::_Ios_Openmode]
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/fstream:871:7: note:   no know
n conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to
'const char*'
functions.cpp: In function 'void saveCreaturesToFile(Creatures*, int)':
functions.cpp:333:35: error: no matching function for call to 'std::basic_fstrea
m<char>::open(std::string&, const openmode&)'
functions.cpp:333:35: note: candidate is:
In file included from Prog4.h:13:0,
                 from functions.cpp:2:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/fstream:871:7: note: void std:
:basic_fstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [wit
h _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std
::_Ios_Openmode]
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/fstream:871:7: note:   no know
n conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to
'const char*'

编辑:第241 -258行我将再次编辑并在几分钟后添加其余内容。

if (menuOption == '2')
    {
        cin.ignore();
        cout << endl << "What is the name of the file you wish to write to? ";
        getline(cin, filename);

        // attempt to open the file.
        dataFile.open(filename, ios::out);

        if (dataFile.fail())    
            cout << endl << endl << "Failed to create " << filename 
                << ", unable to save creatures - file already open?" << endl << endl;
        else
        {
            cout.rdbuf(dataFile.rdbuf());
            redirected = true;
        }
    }

1 个答案:

答案 0 :(得分:4)

如果使用C ++ 11兼容性进行编译,则只能使用fstream直接打开

std::string,在编译时添加-std=c++11标志或通过调用{{打开文件1}}你的字符串的功能。