求解CE
查看原帖
求解CE
639198
Steve_xh楼主2024/6/3 15:00

没写完,到 replace 发现没办法在类里面 new。求解

...cpp: In member function 'virtual Replace* Replace::clone()':
...cpp:141:30: error: cannot allocate an object of abstract type 'Replace'
   return (new ::Replace(*this));
                              ^
...cpp:135:8: note:   because the following virtual functions are pure within 'Replace':
 struct Replace : public Command{
        ^
...cpp:46:15: note:  virtual void Command::recall(int)
  virtual void recall(int) = 0;
               ^
...cpp: In member function 'virtual void Replace::init()':
...cpp:165:12: error: cannot allocate an object of abstract type 'Replace'
    cmd=new Replace;
            ^
...cpp:135:8: note:   since type 'Replace' has pure virtual functions
 struct Replace : public Command{
        ^
...cpp:168:12: error: expected type-specifier before 'Activate'
    cmd=new Activate;
            ^
...cpp:168:12: error: expected ';' before 'Activate'
...cpp:171:12: error: expected type-specifier before 'Trigger'
    cmd=new Trigger;
            ^
...cpp:171:12: error: expected ';' before 'Trigger'

code:

#include<bits/stdc++.h>
using namespace std;
int n,m,k;
struct Counter{
	int now;
	Counter(){}
	inline void init(){now=k;}
	void operator--(){if(!--now)exit(0);}
	void operator--(int){return --*this;}
}cnt;
const string commands[]={
	"",
	"SLACKOFF",
	"MOVE",
	"SWAP",
	"MIRROR",
	"REPLACE",
	"ACTIVATE",
	"TRIGGER"
};
enum{
	SLACKOFF=1,
	MOVE	=2,
	SWAP	=3,
	MIRROR	=4,
	REPLACE	=5,
	ACTIVATE=6,
	TRIGGER	=7
};
inline string getcmdtype(int t)
 {return commands[t];}
inline int getcmdtype(string s)
 {return s[0]=='S'?(s[1]=='L'?SLACKOFF:SWAP):(s[0]=='M'?(s[1]=='O'?MOVE:MIRROR):(s[0]=='R'?REPLACE:(s[0]=='A'?ACTIVATE:TRIGGER)));}
struct Command;
struct Robot{
	Robot(){}
	int id,l,r;
	vector<Command*> c;
}rob[105];
struct Command{
	int type,x,y,z;
	bool h,t;
	Command(){}
	virtual ~Command() = 0;
	virtual void init() = 0;
	virtual void recall(int) = 0;
	virtual Command *clone() = 0;
	virtual Command *mirror() = 0;
};
struct Slackoff : public Command{
	Slackoff(){}
	~Slackoff(){}
	inline void init(){
		type=SLACKOFF;
		x=y=z=h=t=0;
	}
	inline void recall(int x){
		cout<<"Robot "<<x<<" slacks off.\n";
		--cnt;
	}
	inline Slackoff *clone(){
		return (new Slackoff(*this));
	}
	inline Slackoff *mirror(){
		return this->clone();
	}
};
struct Move : public Command{
	Move(){}
	~Move(){}
	inline void init(){
		type=MOVE;
		cin>>h>>z;
		x=y=t=0;
	}
	inline void recall(int x){
		int &hand=(h?(rob[x].r):(rob[x].l));
		cout<<"Robot "<<x<<" moves its "<<(h?"right":"left")<<" hand towards Robot "
		<<((hand+=z)%=n)
		<<".\n";
		--cnt;
	}
	inline Move *clone(){
		return (new Move(*this));
	}
	inline Move *mirror(){
		Move *reans=this->clone();
		reans->h^=1;
		return reans;
	}
};
struct Swap : public Command{
	Swap(){}
	~Swap(){}
	inline void init(){
		type=SWAP;
		cin>>h>>x>>y;
		z=t=0;
	}
	inline void recall(int id){
		cout<<"Robot "<<x<<" swaps a line of command with Robot "<<(h?(rob[id].r):(rob[id].l))<<".\n";
		swap(rob[id].c[(this->y)-1],rob[(h?(rob[id].r):(rob[id].l))].c[(this->x)-1]);
		--cnt;
	}
	inline Swap *clone(){
		return (new Swap(*this));
	}
	inline Swap *mirror(){
		Swap *reans=this->clone();
		reans->h^=1;
		return reans;
	}
};
struct Mirror : public Command{
	Mirror(){}
	~Mirror(){}
	inline void init(){
		cin>>h>>x;
		y=z=t=0;
	}
	inline void recall(int id){
		cout<<"Robot "<<id<<" modifies a line of command of Robot "<<(h?(rob[id].r):(rob[id].l))<<".\n";
		rob[(h?(rob[id].r):(rob[id].l))].c[(this->x)-1]=(rob[(h?(rob[id].r):(rob[id].l))].c[(this->x)-1])->mirror();
		--cnt;
	}
	inline Mirror *clone(){
		return (new Mirror(*this));
	}
	inline Mirror *mirror(){
		Mirror *reans=this->clone();
		reans->h^=1;
		return reans;
	}
};
struct Replace : public Command{
	Command *cmd;
	Replace(){}
	~Replace(){}
//	inline Replace *clone() override;
	inline Replace *clone(){
		return (new ::Replace(*this));
	}
	inline Replace *mirror(){
		Replace *reans=this->clone();
		reans->h^=1;
		return reans;
	}
	inline void init(){
		string cmt;
		cin>>h>>x>>cmt;
		switch(getcmdtype(cmt)){
		case SLACKOFF:
			cmd=new Slackoff;
			break;
		case MOVE:
			cmd=new Move;
			break;
		case SWAP:
			cmd=new Swap;
			break;
		case MIRROR:
			cmd=new Mirror;
			break;
		case REPLACE:
			cmd=new Replace;
			break;
		case ACTIVATE:
			cmd=new Activate;
			break;
		case TRIGGER:
			cmd=new Trigger;
			break;
		}
		cmd->init();
	}
};
struct Activate : public Command{
	
};
struct Trigger : public Command{
	
};
int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    cin>>n>>m>>k;
    cnt.init();
	
	return 0;
}
2024/6/3 15:00
加载中...