错误:没有匹配的函数调用我的类对象
error: No matching function for call to my class object
也许我初始化它不正确?我很确定这就是我们老师教我们怎么做的。但我被难住了。我试图使用该函数作为访问器,并获得数据,并显示已经存储在结构体"坐标"下的成员"入口"
头文件:
#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’
是的…我被难住了……任何帮助将非常感激!我已经想了一个小时了
访问main中变量Entrance
的成员变量。我想你需要的是这个
coordinate MazeClass::GetEntrance()
{
return Entrance;
}
并相应地修改头文件中的函数原型。
通过此修改,您的以下语句将正常工作(检查()
是否调用col
)
cout << "The entrance is at: " << maze.GetEntrance().row << " " << maze.GetEntrance().col << endl;
更改您的GetExit
函数也像这样使用它
错误信息实际上告诉您哪里出错了:
error: no matching function for call to ‘MazeClass::GetEntrance()’
^^^^^
您提供的函数是:
void MazeClass::GetEntrance(coordinate Entrance)
^^^^^^^^^^^^^^^^^^^^^^
注意上面的^^^^^^
正如你所看到的,两个函数是不一样的!
您声明并定义了一个以coordinate
对象为参数的方法,但是您调用的方法不带任何参数,显然编译器没有找到任何这样的方法并适当地抱怨。
您对GetEntrance
的定义说它需要一个(类型为coordinate
的)参数。
在你试图调用它的地方,你没有传递参数,所以它会寻找其他可以不带参数调用的重载。
还要注意,您的maze.GetEntrance.col
可能需要改为maze.GetEntrance().col
。
1)您正在调用maze.GetEntrance()
,但MazeClass
没有GetEntrance()
方法。只有MazeClass::GetEntrance(coordinate)
.
2)你对maze.GetEntrance().row
的调用表明你期望MazeClass::GetENtrance()
返回一些东西。当前返回void
, void
没有成员row
。
- 为什么我的运算符 + 重载尽管是通过引用传递的,但仍调用我的复制构造函数?
- 什么时候可以使用常量装饰调用我的重载函数?
- 为什么当我做复制和交换习语时不调用我的复制构造函数?
- 钩/绕道 d3d9 (现在/结束场景) - 似乎调用我的函数然后崩溃
- 为什么在这种情况下不调用我的虚拟函数实现?
- 当我从头文件和实现文件调用我的函数到我的主文件时,我没有得到任何输出
- 如何制作指针来调用我的成员函数
- 调用自动构造函数:为什么我的类型不完整
- 为什么在这种情况下不调用我的移动构造函数?(首先分配 r 值引用并创建对象)
- C++模板:无法从 main.cpp 调用我的函数
- 为什么我继承的构造函数调用我的基本默认构造函数
- c++ Glut 显示需要调用我的绘制函数,但我无法传递任何参数
- 为什么当我调用 print() 函数时我的字符串不会输出?
- 不明确的构造函数调用(我认为)
- 如何从对象中调用我的set回调函数
- C++ / 多态性 / 虚函数 / 为什么我的子类的函数没有被调用?
- 当用gdb调试我的c++代码时,函数调用中的变量值是荒谬的
- 我想用字符串参数调用我的函数
- 为什么从 win32 计时器回调抛出时不调用我的析构函数?
- 错误:没有匹配的函数调用我的类对象