C++ 程序无法编译
这是密码C++ 程序无法编译,c++,compiler-errors,fstream,ifstream,ofstream,C++,Compiler Errors,Fstream,Ifstream,Ofstream,这是密码 #include <iostream> #include <cstdlib> #include <fstream> using namespace std; class sudoku { public: void read(ifstream ifs); void write(ofstream ofs); private: int puzzle[9][9]; }; void sud
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
class sudoku {
public:
void read(ifstream ifs);
void write(ofstream ofs);
private:
int puzzle[9][9];
};
void sudoku::read(ifstream ifs){
int row;
int col;
int value;
int linenum = 1;
bool error = false;
while(ifs >> row){
ifs >> col;
ifs >> value;
if(row < 0 || row > 8){
cerr << "Incorrect Row Value: " << row << " Line Number: " << linenum;
error = true;
}
if(col < 0 || col > 8){
error = true;
cerr << "Incorrect Col Value: " << col << " Line Number: " << linenum;
}
if(value < 1 || value > 9){
error = true;
cerr << "Invalid Value: " << value << " Line Number: " << linenum;
}
if (! error)
puzzle[row][col] = value;
error = false;
}
}
void sudoku::write(ofstream ofs){
for (int i = 0; i < 9; i++){
for (int j = 0; j < 0; j++){
if(puzzle[i][j] != 0){
ofs << i << ' ' << j << ' ' << puzzle[i][j] << endl;
}
}
}
}
int main(int argc, char* argv[]){
sudoku sudopuzzle;
string filename = argv[1];
int found = filename.find(".txt");
if(found == filename.npos) {
cout << "No .txt extension" << endl;
return 0;
}
ifstream ifs;
ifs.open(filename.c_str());
sudopuzzle.read(ifs);
ifs.close();
filename.resize(filename.size()-4);
filename.append("_checked.txt");
ofstream ofs;
ofs.open(filename.c_str());
sudopuzzle.write(ofs);
ofs.close();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
类数独{
公众:
无效读取(ifs);
无效写入(ofs流);
私人:
智力拼图[9][9];
};
void数独::读取(ifstream ifs){
int行;
int col;
int值;
int linenum=1;
布尔误差=假;
而(ifs>>行){
ifs>>col;
ifs>>值;
如果(行<0 | |行>8){
cerr您应该传递对流对象的引用:
class sudoku {
public:
void read(ifstream &ifs);
void write(ofstream &ofs);
private:
int puzzle[9][9];
};
void sudoku::read(ifstream &ifs){
// sudoku::read code here
}
void sudoku::write(ofstream &ofs){
// sudoku::write code here
}
此更改是必需的,因为流的ifstream
和都有一个=delete
复制构造函数。(帽子提示:@awesomeyi)因为ifstream
和流的有一个=deleted
复制构造函数我实际上有一个简单的问题。我如何将我的2d数组初始化为零?@user2789171你可以对循环使用嵌套的,也可以使用memset(puzzle,0,sizeof(puzzle))
您也可以在数独
构造函数的初始化列表中默认初始化数组。例如,您可以添加以下默认构造函数:数独():board(){}
。更正:这应该是“值初始化”,而不是“默认初始化”。