#include "Board.hpp"
#include <iostream>
using namespace std;
Board::Board (const char* filename){
filename = "puz1.txt";
Board::fin (filename);
if(!fin) fatal("Error in opening the file");
}
这是我的 cpp 文件...我的 hpp 文件是:
#ifndef BOARD_H
#define BOARD_H
#include <iostream>
using namespace std;
#include "tools.hpp"
#include "square.hpp"
class Board {
private:
SqState bd[81];
ifstream fin;
public:
Board(const char* );
ostream& print(ostream& );
};
inline ostream& operator <<(ostream& out, Board& b) { return b.print(out);}
#endif //Board.hpp
编译时出现以下错误:
cpp
filename = "puz1.txt"
中的行出错。 错误是:const char* shadows a //parameter.
cpp
Board::fin (filename); 中的一行错误
错误是:no match call to //(std::basic_ifstream})
我该如何修复它们?
最佳答案
只能初始化fin
在构造函数初始化列表中。您还需要 #include <fstream>
.这会起作用:
Board::Board (const char* filename): fin(filename)
{
....
}
不清楚为什么要设置 filemane
与构造函数中传递的内容不同。如果你想要一个默认参数,使用
Board::Board (const char* filename="puz1.txt"): fin(filename) {}
关于c++ - const char* 在构造函数中的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23228905/