错误:没有用于调用类对象

时间:2015-12-12 03:59:19

标签: c++ c++11 codeblocks

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

>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;
}

Thing标题

#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;
}

代理商标题

#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;
}

玩家标题

#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;
}

我老老实实地难以接受下一步去哪儿。非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

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

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

您需要将代理构造函数更改为:

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

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

答案 1 :(得分:0)

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

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