我还没有写完,但是第 133 行(将近末尾)这一行
if (CmdName == "SLACKOFF") cmd = new SlackOff((CommandData)cmddata);
出错,两个错误(我用的 Visual Studio):
1. error C2665: “SlackOff::SlackOff”: 没有重载函数可以转换所有参数类型
2. 可能是“SlackOff::SlackOff(SlackOff &&)”
main.cpp(133,49):
“SlackOff::SlackOff(SlackOff &&)”: 无法将参数 1 从“CommandData”转换为“SlackOff &&”
main.cpp(133,50):
原因如下: 无法从“CommandData”转换为“SlackOff”
main.cpp(133,50):
没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
求帮助……
完整代码:
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
int n;
class Command;
class Robot
{
public:
int Index = -1, LeftHand = -1, RightHand = -1;
vector<Command*> commands;
void print() const
{
printf("Index = %d\tLeftHand = %d\tRightHand = %d\nCommand:", Index, LeftHand, RightHand);
for (int i = 0; i < commands.size(); i++)
{
printf("Command id=%d on %p\n", i, commands[i]);
}
}
};
Robot robots[105];
class Tools
{
public:
static int &LorR(Robot &rbt, int h)
{
if (h == 0) return rbt.LeftHand;
else if (h == 1) return rbt.RightHand;
else
{
printf("Error in function 'LorR'!\n'h' = %d is invaild!\n'h' must in {0,1}!\nRobot infomation:\n", h);
rbt.print();
while (true)
{
printf("");
}
}
}
static int &LorR(int id, int h) { return LorR(robots[id], h); }
};
class CommandData
{
public:
int robotid = -1, h = -1, x = -1, y = -1, z = -1;
string COMMANDNAME = "None";
Command* COMMAND = nullptr;
void print()
{
printf("robotid = %d, h = %d, x = %d, y = %d, z = %d, <COMMANDNAME> = %s, <COMMAND> = %p", robotid, h, x, y, z, COMMANDNAME.c_str(), COMMAND);
}
};
class Command
{
public:
CommandData data;
Command() = delete;
Command(const CommandData &dt) : data(dt) {}
virtual void run()
{
printf("Error!\nCommand{");
data.print();
printf("}::run() shouldn't be called!\n");
while (true)
{
printf("");
}
}
};
class SlackOff : public Command
{
public:
void run() { printf("Robot %d slacks off.\n", data.robotid); }
};
class Move : public Command
{
public:
void run()
{
int h = data.h, z = data.z;
Robot& rbt = robots[data.robotid];
printf("Robot %d moves its ", rbt.Index);
int& rbtHand = Tools::LorR(rbt, h);
rbtHand = (rbtHand + z) % n;
if (h == 0) printf("left");
else printf("right");
printf(" hand towards Robot %d.\n", h == 0 ? rbt.LeftHand : rbt.RightHand);
}
};
class Swap : public Command
{
public:
void run()
{
int h = data.h, x = data.x, y = data.y;
Robot& me = robots[data.robotid];
Robot& it = robots[Tools::LorR(data.robotid, h)];
swap(me.commands[y - 1], it.commands[x - 1]);
printf("Robot %d swaps a line of command with Robot %d.\n", me.Index, it.Index);
}
};
//TODO:写Swap之后的,写Robot的运行,写输入输出,调试,AC
int main()
{
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; i++)
{
Robot& rbt = robots[i];
rbt.Index = i;
scanf("%d%d", &rbt.LeftHand, &rbt.RightHand);
for (int j = 1; j <= m; j++)
{
string CmdName;
cin >> CmdName;
CommandData cmddata;
cmddata.robotid = i;
Command* cmd = nullptr;
if (CmdName == "SLACKOFF") cmd = new SlackOff((CommandData)cmddata);
}
}
return 0;
}