也许我初始化不正确?Preety肯定这就是我们的老师向我们展示如何做到这一点的方式......但我很难过。我正在尝试将该函数用作访问器并获取数据并显示已存储在成员“入口”下的结构“坐标”中的内容
头文件:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
#include "type.h"
const int MAX = 50;
#ifndef MazeClass_H
#define MazeClass_H
class MazeClass
{
private:
SquareType Maze[MAX][MAX];
coordinate Entrance, Exit;
int height, width;
public:
MazeClass();
void ReadMaze(ifstream&);
void DisplayMaze();
void GetEntrance(coordinate);
void GetExit(coordinate);
void MarkVisited(coordinate);
void MarkPath(coordinate);
bool IsWall(coordinate);
bool IsClear(coordinate);
bool IsPath(coordinate);
bool IsVisited(coordinate);
bool IsExit(coordinate);
bool IsInMaze(coordinate);
};
#endif
实现文件:
#include "MazeClass.h"
#include <iostream>
#include <cstdlib>
char maze[50][50];
MazeClass::MazeClass()
{
}
void MazeClass::ReadMaze(ifstream& myIn)
{
int x, y;
myIn >> x;
myIn >> y;
height = x;
width = y;
myIn >> x;
myIn >> y;
Entrance.row = x;
Entrance.col = y;
myIn >> x;
myIn >> y;
Exit.row = x;
Exit.col = y;
myIn.ignore(100, '\n');
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width + 1; k++)
{
myIn.get(maze[i][k]);
if(maze[i][k] == '*')
Maze[i][k] == Wall;
else
Maze[i][k] == Clear;
}
}
}
void MazeClass::DisplayMaze()
{
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width + 1; k++)
{
cout << maze[i][k];
}
}
}
void MazeClass::GetEntrance(coordinate Entrance)
{
}
void MazeClass::GetExit(coordinate Exit)
{
}
和我的文件尝试使用它:
#include "MazeClass.h"
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char *argv[])
{
MazeClass maze;
ifstream myIn;
int x,y;
string filename = argv[1]; // command line arguement stuff
myIn.open(filename.c_str());
maze.ReadMaze(myIn); //reads in the maze from a data file
maze.DisplayMaze();
cout << "The entrance is at: " << maze.GetEntrance().row << " " << maze.GetEntrance.col << endl;
myIn.close();
return 0;
}
我得到这个错误:
ola4A1.cc:17: error: no matching function for call to ‘MazeClass::GetEntrance()’
MazeClass.h:21: note: candidates are: void MazeClass::GetEntrance(coordinate)
ola4A1.cc:17: error: ‘maze.MazeClass::GetEntrance’ does not have class type
我试过这样放置坐标:
#include "MazeClass.h"
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char *argv[])
{
MazeClass maze;
ifstream myIn;
int x,y;
string filename = argv[1]; // command line arguement stuff
myIn.open(filename.c_str());
maze.ReadMaze(myIn); //reads in the maze from a data file
maze.DisplayMaze();
cout << "The entrance is at: " << maze.GetEntrance(coordinate).row << " " << maze.GetEntrance(coordinate).col << endl;
myIn.close();
return 0;
}
但我得到了错误:
ola4A1.cc: In function ‘int main(int, char**)’:
ola4A1.cc:17: error: expected primary-expression before ‘)’ token
ola4A1.cc:17: error: expected primary-expression before ‘)’ token
之后......我像这样在“入口”中添加了......
#include "MazeClass.h"
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char *argv[])
{
MazeClass maze;
ifstream myIn;
int x,y;
string filename = argv[1]; // command line arguement stuff
myIn.open(filename.c_str());
maze.ReadMaze(myIn); //reads in the maze from a data file
maze.DisplayMaze();
cout << "The entrance is at: " << maze.GetEntrance(coordinate Entrance).row << " " << maze.GetEntrance(coordinate Entrance).col << endl;
myIn.close();
return 0;
}
它给了我这个错误......
ola4A1.cc: In function ‘int main(int, char**)’:
ola4A1.cc:17: error: expected primary-expression before ‘Entrance’
ola4A1.cc:17: error: expected primary-expression before ‘Entrance’
所以是的......我很难过......任何帮助将不胜感激!我一直在尝试解决这个问题一个小时:(