【发布时间】:2013-03-25 04:50:28
【问题描述】:
我正在尝试将一个名为“坐标”的结构传递给一个具有 2 个 int 的函数,因此当调用该函数时,可以输入整数作为函数的参数。示例:
这是我想要使用的结构:
struct coordinate
{
int row;
int col;
};
函数原型如下所示:
bool IsInMaze(coordinate);
我想要做的是这样调用函数......
IsInMaze(1,5);
所以 1 将是行,5 将是列。该功能基本上是检查坐标(int)是否在特定尺寸的大小范围内......像这样
bool MazeClass::IsInMaze(coordinate location)
{
if(location.row < 0 || location.row > height || location.col < 0 || location.col > width)
return false;
else
return true;
}
但我收到此错误...
ola4A1.cc: In function \u2018int main(int, char**)\u2019:
ola4A1.cc:30: error: no matching function for call to \u2018MazeClass::IsInMaze(int, int)\u2019
MazeClass.h:30: note: candidates are: bool MazeClass::IsInMaze(coordinate)
ola4A1.cc:35: error: no matching function for call to \u2018MazeClass::IsInMaze(int, int)\u2019
MazeClass.h:30: note: candidates are: bool MazeClass::IsInMaze(coordinate)
我从来没有真正被教导过,所以我认为我只是不理解这一点。我怎样才能编写这个函数,我可以按照我想要的方式调用它?谢谢。
【问题讨论】:
-
像这样调用
coordinate c {1, 2}; IsVisited(c);或声明重载版本:bool IsVisited(int, int)。 -
你的函数被称为
IsVisited或IsInMaze? -
抱歉,我在 RedBaron 上复制了错误的函数原型,它是“IsInMaze”
-
试试
IsInMaze({1,5});,假设你有一个兼容 C++0x 的编译器。 -
而且大多数时候,你想声明
IsInMaze(coordinate &)而不是IsInMaze(coordinate)。