fstream库出错

时间:2016-02-08 13:16:06

标签: c++ fstream

我只是想以书面形式打开文件并将其放入变量中,并出现错误:

Include/FileStream/FileStream.cpp: In function 'void log(std::__cxx11::string)': Include/FileStream/FileStream.cpp:26:5: error: request for member 'close' in 'log', which is of non-class type 'void(std::__cxx11::string) {aka void(std::__cxx11::basic_string<char>)}'  log.close;
     ^ Include/FileStream/FileStream.cpp: In function 'void firstName_read(bool)': Include/FileStream/FileStream.cpp:36:18: error: return-statement with a value, in function returning 'void' [-fpermissive]
           return firstname;
                  ^ Include/FileStream/FileStream.cpp:42:18: error: return-statement with a value, in function returning 'void' [-fpermissive]
           return firstname;

并且存在制造麻烦的文件:

#include "FileStream.h"
void log(string argument)
{
logFile.open ("log.txt", fstream::out | fstream::app);

logFile << argument;

log.close;
}

void firstName_read (bool boyOrGirl)
{

if (boyOrGirl == true)
     {
          firstname.open("Name/FirstName_Male.txt", fstream::in);

          firstname.close;
          return firstname;
     }
else
     {
          firstname.open("Name/FirstName_Female.txt", fstream::in);
          firstname.close;
          return firstname;

     }
}

谢谢...如果你想要我在github上托管项目的所有文件:

https://github.com/calmunicorn/virtualsociety

4 个答案:

答案 0 :(得分:4)

close不是ifstream属性,它是一种方法。使用firstname.close()来调用该函数而不是firstname.close

另外,void函数无法返回值,而firstname .... log.close也应该是logFile.close() ....实际上有错误的音调在你的代码中。

您可能希望从一段简单的代码开始,使其工作,然后再扩展它....

答案 1 :(得分:4)

在您的函数log中,文件名为logFile。但你试试log.close。这真的让编译器感到困惑;你不能关闭一个功能!使用logfile.close()

答案 2 :(得分:0)

void firstName_read( /*...*/是一个不返回void的函数。如果您想返回std::string,请将其定义为std::string firstName_read( /*...*/

答案 3 :(得分:0)

谢谢你们所有人帮助我解决了这个问题,现在我遇到了一些其他问题,但我会为那个XD开一个新帖子