const char*在构造函数中的用法
const char* usage in constructor
#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*对//参数进行阴影处理。
cpp
Board::fin (filename);
中的行出错错误为:对//(std::basic_ifstream}(的无匹配调用
我该如何修复它们?
您只能在构造函数初始化列表中初始化fin
。您还需要#include <fstream>
。这将起作用:
Board::Board (const char* filename): fin(filename)
{
....
}
目前还不清楚为什么要将filemane
设置为与构造函数中传递的内容不同的内容。如果您想要一个默认参数,请使用
Board::Board (const char* filename="puz1.txt"): fin(filename) {}
关于第一个错误:
filename = "puz1.txt";
您应该将filename
作为参数传递,而不是将其分配给它。如果您只需要使用"puz1.txt"
,则使用than而不是filename
。
第二个错误:
Board::fin (filename);
不能像那样初始化ifstream
对象。只需拨打open()
即可。
fin.open("puz1.txt");
if(fin.is_open()) // you can pass additional flags as the second param
{
}
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 构造函数的用法
- 使用指针复制构造函数用法
- CPP/C 中的常量用法和结构构造函数中的澄清
- 复制构造函数方法的用法
- 参数和成员变量在构造函数中的用法
- C 中的构造函数和破坏者的内联用法
- 为什么内存泄漏只发生在赋值运算符重载的情况下,而不是在复制构造函数中,以及复制和交换习惯用法如何解决它
- 使用检测习惯用法来确定类型是否具有具有特定签名的构造函数
- C++:复制和交换习惯用法,替代构造函数
- 带有智能指针的虚拟构造函数习惯用法
- 如何实现拷贝交换习惯用法的复制构造函数
- 对象构造函数"settings"习惯用法
- c++单例用法:编译器抱怨私有构造函数
- 移动构造函数的预期用法未发生
- const char*在构造函数中的用法