【问题标题】:no matching constructor for initialization of 'ifstream''ifstream' 的初始化没有匹配的构造函数
【发布时间】:2017-12-10 22:08:12
【问题描述】:

我正在尝试编写一个读取文件并将其放入结构的代码。但是,当我运行我的代码时,它给了我一个不寻常的错误,我看不出我的代码有什么问题。

结构图 {

int x;
int y;
char symbol;
int id;
string fullname;
bool visited;
}; 

void locationFile(ifstream filename, vector<Map> &vmap, int rows, int 
col) 

{
Map map;

while (filename >> map.x >> map.y >> map.symbol >> map.id)
{
    if (map.x> rows || map.x< 1 || map.y> col || map.y<1)
    {
        cout << map.id << " out of range -  ignoring" << endl;
        map.visited = true;
    }
    else
    {
        vmap.push_back(map);
    }
}
}

void namesFile(ifstream names, vector<Map>& vmap)
{
Map map;

while(names >> map.id>> map.symbol)
{
    vmap.push_back(map);
}
}

int main()
{
vector<Map> info;
string location;
string names;
getFilenames(location, names);

//open files
ifstream inL;
inL.open(location.c_str());

string journey= "journey.txt";
ofstream fout(journey.c_str());

int rows, col, sx, sy, ex, ey;
inL >> rows >> col >> sx >> sy >> ex >> ey;

locationFile(inL, info, rows, col);

inL.close();

ifstream inN;
inN.open(names.c_str());
namesFile(inN, info);
inN.close();



vector< vector<string> > grid;
grid= createGrid(info, rows, col, sx, sy, ex, ey);

//print out grid
for(int h=0; h< grid.size(); h++)
{
    for(int g=0; g<grid.size(); g++)
        {
            fout << grid[h][g];
        }
}

结果显示是根据从文件中读取的信息创建的网格/

) ^ /Library/Developer/CommandLineTools/usr/include/c++/v1/ios:313:5:注意: 在这里声明为私有 ios_base(const ios_base&); // = 删除; ^ /Library/Developer/CommandLineTools/usr/include/c++/v1/iosfwd:131:32:注意: 'std::__1::basic_ios' 的隐式复制构造函数首先需要 这里 类_LIBCPP_TEMPLATE_VIS basic_ifstream; -->

【问题讨论】:

  • ifstream 不可复制,不能按值传递。而是通过引用传递。
  • 您正试图将 ifstream 按值传递给您的函数,这会导致它们被复制。不允许这样的操作(复制流对象)。而是通过引用传递它们。

标签: c++ struct constructor ifstream


【解决方案1】:
void locationFile(ifstream filename, vector<Map> &vmap, int rows, int col)

void namesFile(ifstream names, vector<Map>& vmap)

这两个函数采用ifstream 的值。要传递变量,必须复制它。但是,ifstream's copy constructor is deleted

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2017-06-27
    • 1970-01-01
    • 2022-11-17
    • 2018-11-14
    相关资源
    最近更新 更多