我一直得到三个与“调用隐式删除的____默认构造函数有关的错误。有人会碰巧知道这是为什么吗?
/ Users / vivekreddy / Desktop / Vivek / School / Spring 2016 / CS32 / project 3 / project 3 / Player.cpp:114:18:调用隐式删除的'BadPlayerImpl'默认构造函数
#include "provided.h"
#include <iostream>
#include <string>
using namespace std;
class HumanPlayerImpl:public Player
{
public:
virtual bool isInteractive() const { return true;};
int chooseMove(const Scaffold& s, int N, int color);
};
class BadPlayerImpl: public Player
{
public:
int chooseMove(const Scaffold& s, int N, int color);
};
class SmartPlayerImpl: public Player
{
public:
virtual bool isInteractive() const { return true;};
int chooseMove(const Scaffold& s, int N, int color);
};
//implementations
//put virtual in front of implementations only or declarations here as well?
int HumanPlayerImpl::chooseMove(const Scaffold& s, int N, int color) //goal is to get N in a row, return the column necessary to put the checker so we could do that. otherwise return -1. can make the column be arbitrary
{
//algorithm is inputting N so maybe don't need this check?
if (N>s.cols()||N<=0) {
cout<<"Enter a number within the valid range of columns you specified";
return -1;
}
if(s.numberEmpty()==0)
{
return -1;
}
int columnnum;
while (columnnum<=s.cols()||columnnum<=0) {
cout<<"Enter column number of your move";
cin>>columnnum;
if (columnnum<=s.cols()) {
return columnnum;
}
cout<<"Column number not valid ";
}
return -1; //OK?
}
int BadPlayerImpl::chooseMove(const Scaffold& s, int N, int color)
{
if ((N>=s.cols()&&N>=s.levels())||N<=0) { //make sure this works
cout<<"Enter a number within the valid range of columns you specified";
return -1;
}
for (int j=0; j<s.cols();j++) {
if (s.checkerAt(j,0)==VACANT) {
return j;
}
}
return -1; //see if this OK
}
int SmartPlayerImpl::chooseMove(const Scaffold& s, int N, int color)
{
return -1; // This is not always correct; it's just here to compile
}
//******************** Player derived class functions *************************
// These functions simply delegate to the Impl classes' functions.
// You probably don't want to change any of this code.
HumanPlayer::HumanPlayer(string nm)
: Player(nm)
{
m_impl = new HumanPlayerImpl; //error is here
}
HumanPlayer::~HumanPlayer()
{
delete m_impl;
}
int HumanPlayer::chooseMove(const Scaffold& s, int N, int color)
{
return m_impl->chooseMove(s, N, color);
}
BadPlayer::BadPlayer(string nm)
: Player(nm)
{
m_impl = new BadPlayerImpl; //error is here
}
BadPlayer::~BadPlayer()
{
delete m_impl;
}
int BadPlayer::chooseMove(const Scaffold& s, int N, int color)
{
return m_impl->chooseMove(s, N, color);
}
SmartPlayer::SmartPlayer(string nm)
: Player(nm)
{
m_impl = new SmartPlayerImpl; //error is here
}
SmartPlayer::~SmartPlayer()
{
delete m_impl;
}
int SmartPlayer::chooseMove(const Scaffold& s, int N, int color)
{
return m_impl->chooseMove(s, N, color);
}
下面是我对类Player的声明,它是基类:
class Player
{
public:
Player(std::string nm) : m_name(nm) {}
virtual ~Player() {};
std::string name() const { return m_name; };
virtual bool isInteractive() const { return false; }
virtual int chooseMove(const Scaffold& s, int N, int color) = 0;
// We prevent any kind of Player object from being copied or assigned by
// making the copy constructor and assignment operator unavailable in
// the base class (and thus in any derived class).
Player(const Player& other) = delete;
Player& operator=(const Player& other) = delete;
private:
std::string m_name;
};
答案 0 :(得分:3)
因为您在基类中声明了一个构造函数,特别是Player::Player(std::string nm)
,所以编译器不可能为您的子类提供隐式默认构造函数,因为您的父类Player
不能是默认的首先建造。
在子类中提供构造函数或继承基类的构造函数(C ++ 11)。 E.g:
BadPlayerImpl::BadPlayerImpl(std:string nm)
: Player(nm)
{
...
}
答案 1 :(得分:0)
这很可能是因为代码中的某个地方是复制构造或分配给BadPlayerImpl
的实例,并且您明确删除了其超类的复制构造函数和赋值运算符。
答案 2 :(得分:0)
通常会为类自动创建默认构造函数,但是有关于何时不创建它的规则(例如,当您创建另一个构造函数时)。你在哪里&#34; //错误在这里&#34;你正在调用Sub-Class的默认构造函数,它将调用Base-Class的(在这种情况下,不存在的)默认构造函数。