Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;类的头文件中的初始值设定项列表_C++ - Fatal编程技术网

C++ C++;类的头文件中的初始值设定项列表

C++ C++;类的头文件中的初始值设定项列表,c++,C++,我的Filer.h文件中有以下类: #include<string> #include<boost\filesystem.hpp> #include<fstream> using namespace boost::filesystem; #ifndef FILER_H #define FILER_H class Filer() { public : // I have tried many other syntax, but none of th

我的
Filer.h
文件中有以下类:

#include<string>
#include<boost\filesystem.hpp>
#include<fstream>

using namespace boost::filesystem;

#ifndef FILER_H
#define FILER_H

class Filer()
{
public :
    // I have tried many other syntax, but none of them worked.
    Filer() : fileStr();
    std::fstream& fileStr;
};

#endif

我的问题是,在构建
文件管理器
类时,我希望有一个
std::fstream
的实例。我想把这个类分配给
fileStr
变量。那么我应该如何解决我的问题呢?

如果您想创建一个包含在此类中的std::fstream,只需从此行中删除符号:

std::fstream& fileStr;
要做到这一点:

std::fstream fileStr;
fstream将自动构建

如果希望fileStr引用在别处定义的std::fstream,则需要一个构造函数来引用该外部fstream并使用它初始化fileStr引用。该代码如下所示:

Filer(std::fstream & stream) : fileStr(stream) 
{ }
std::fstream& fileStr;
Filer::Filer(std::fstream & stream) 
      : fileStr(stream)
{
}
如果在cpp文件中定义构造函数,则不能指定bool返回值,并且必须具有参数,使其看起来像:

Filer(std::fstream & stream) : fileStr(stream) 
{ }
std::fstream& fileStr;
Filer::Filer(std::fstream & stream) 
      : fileStr(stream)
{
}

您有两个错误,第一个错误是您现在应该在声明中有初始值设定项列表。另一个错误是,如果需要引用,则需要将
std::fstream
引用作为参数传递给构造函数,并将其用于初始值设定项

如果构造函数很简单,如为类显示的构造函数,则可以将其设置为内联函数:

Filer(std::fstream& str) : fileStr(str)
{}

那么.cpp文件中的定义应该是什么?