error: No matching function for call to class object

error: No matching function for call to class object

每次尝试构建我的项目时,我都会收到相同的错误:

>error: no matching function for call to 'Agent::Agent()'
>note: candidates are: Agent::Agent(std::string, room*)            
>note: Agent::Agent(const Agent&)

最初我以为我输入了错误的值,但即使在看似更正之后,我仍然遇到同样的错误。

主要

#include <iostream>
#include <string>
#include <sstream>
#include "room.h"
#include "Thing.h"
//#include "Agent.h"
//#include "Grue.h"
//#include "Player.h"

using namespace std;


int main()
{
srand(time(NULL));

room *entrance = new room("Entrance","A wide open entrance...", 100);
room *hallway = new room("Hallway","A long hallway...", 50);
room *ballroom = new room("Ballroom","A huge ballroom...", 200);
room *garden = new room("Garden","A lush garden...", 150);


entrance->link("south", hallway);
hallway->link("north", entrance);
hallway->link("east", ballroom);
ballroom->link("west", hallway);
ballroom->link("east", garden);

hallway->printLinked();


while(true)
{
    for(int i = 0; i < agents.size(); i++)
    {
        bool ok = agents[i]->act();
        if(!ok)
        {
            cout << "Game quits." << endl;
            return 0;
        }
    }
}

Player *josh = new Player("Josh", entrance);
Player *tracy = new Player("Tracy", entrance);
game.addAgent(josh);
game.addAgent(tracy);

cout << "Welcome!" << endl;

// the step() function in the Game class will eventually
// return false, when a player chooses to quit;
// this tiny "while" loop keeps asking the game.step()
// function if it is false or true; the effect is
// that the step() function is called repeatedly
// until it returns false
while(game.step());

return 0;
}

东西header

#ifndef THING_H
#define THING_H

#include <iostream>
#include <string>
#include "room.h"

class room;

class Thing
{
private:
    std::string name, desc;
protected:
    room* cur_room;
public:

    Thing(std::string _name, std::string _desc);
    std::string getName();
    std::string getDesc();
    int getSize();

};

#endif // THING_H

事物 cpp

#include "Thing.h"

Thing::Thing(std::string _name, std::string _desc)
{
name = _name;
desc = _desc;
cur_room = NULL;
}

std::string Thing::getName()
{
return name;
}

std::string Thing::getDesc()
{
return desc;
}

int Thing::getSize()
{
return size;
}

代理header

#ifndef AGENT_H
#define AGENT_H


#include "Thing.h"
#include <iostream>
#include <string>
#include "room.h"

class Agent : public Thing
{
protected:
    //bool walk(std::string exit);
    room *cur_room;
    std::string name;
public:
    Agent(std::string _name, room *_cur_room);
    void get_curroom();
    virtual bool act() = 0;
    std::string getName() { return name; }
};

#endif // AGENT_H

代理 cpp

#include "Agent.h"

Agent::Agent(std::string _name, room *_cur_room)
{
name = _name;
room = _cur_room;
}

bool Agent::walk(std::string exit)
{
return 0;
}

bool Agent::act()
{
return 0;
}

void Agent::get_curroom()
{
return cur_room;
}

玩家header

#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
#include <string>
#include "Agent.h"
#include "room.h"

class room;

class Player : public Agent
{
private:
std::string name;
protected:
public:
    Player(std::string _name, room *starting_room);
    bool Act();
};

#endif // PLAYER_H

玩家cpp

#include "Player.h"

Player::Player(std::string _name, room *starting_room)
{
name = _name;
cur_room = starting_room;
}

bool Player::Act()
{
std::cout << "Where do you want to go? (or 'quit')"  << std::endl;
}

老实说,我不知道下一步该去哪里。非常感谢任何帮助!

问题在于,在 Player 的构造函数中,您没有调用基础 class Agent 的构造函数,后者没有默认构造函数。要修复它,请调用 Agent 的构造函数(或添加默认构造函数):

Player::Player(std::string _name, room *starting_room)
: Agent(_name, starting_room)
{
// ...
}

在您的 Player<-Agent<-Thing 继承链中,您没有将构造函数参数传递给父级。因此,您需要将 Player 构造函数更改为:

Player::Player(std::string _name, room *starting_room)
    :Agent(_name, starting_room)
{
name = _name;
cur_room = starting_room;
}

并且您需要将 Agent 构造函数更改为:

Agent::Agent(std::string _name, room *_cur_room)
    :Thing(_name, "")
{
name = _name;
room = _cur_room;
}

我在冒号后面添加的部分称为初始化列表。它们的一种用途是调用父 class 的构造函数。在 C++ 中,您必须按名称调用父 class 的构造函数,因为通常没有关键字来引用父 class。