错误:没有匹配的函数调用我的类对象

error: No matching function for call to my class object

本文关键字:函数调用 我的 对象 错误      更新时间:2023-10-16

也许我初始化它不正确?我很确定这就是我们老师教我们怎么做的。但我被难住了。我试图使用该函数作为访问器,并获得数据,并显示已经存储在结构体"坐标"下的成员"入口"

头文件:

#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