c++ - "Call to implicitly deleted default constructor of "错误

标签 c++ debugging inheritance constructor polymorphism

我不断收到三个错误,它们都与“调用 ____ 的隐式删除的默认构造函数有关。有人会碰巧知道这是为什么吗?

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

最佳答案

因为您在基类中声明了构造函数,特别是 Player::Player(std::string nm),编译器不可能为您的子类提供隐式默认构造函数,因为您的父类 Player 一开始就不能默认构造。

要么在子类中提供构造函数,要么继承基类的构造函数 (C++11)。例如:

BadPlayerImpl::BadPlayerImpl(std:string nm)
  : Player(nm)
{
   ...
}

关于c++ - "Call to implicitly deleted default constructor of "错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37233407/

相关文章:

c++ - 将方法添加到与继承方法同名的子类

c++ - 不调用父类(super class)构造函数的多级继承

C++虚函数覆盖

c++ - 如何将 std::vector<char*> 转换为 char**?

c++ - 从 C++ 文件使用 Objective-C++ 文件

.net - pdb 文件实际上是做什么的?

c++ - 视觉 C++ 2005 : How to view the window during a debugging session

c++ - 模板参数类型(对于函数模板): C++

python - 我找不到 python 中的这个圆反弹计算有什么问题

c++ - 关于继承和覆盖的问题