Google Mock:“没有适当的默认构造函数可用”? [英] Google Mock: "no appropriate default constructor available"?

查看:676
本文介绍了Google Mock:“没有适当的默认构造函数可用”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Visual Studio 2010 C ++与googlemock。我试图使用我创建的模拟,我得到编译错误的行上:

Using Visual Studio 2010 C++ with googlemock. I'm trying to use a mock I created and I'm getting the compiler error on the line:

EmployeeFake employeeStub;

错误是:

1>c:\someclasstests.cpp(22): error C2512: 'MyNamespace::EmployeeFake' : no appropriate
default constructor available

EmployeeFake:

EmployeeFake:

class EmployeeFake: public Employee{
 public:
  MOCK_CONST_METHOD0(GetSalary,
      double());
}

员工:

class Employee 
{
public:
    Employee(PensionPlan *pensionPlan, const char * fullName);
    virtual ~Employee(void);

    virtual double GetSalary() const;
}



我认为问题是基类没有默认构造函数,但我应该如何解决这个问题?我需要添加一个默认的构造函数到我的基类吗?或者我需要添加一个构造函数到我的mock类?

I gather that the problem is that the base class doesn't have a default constructor but how should I fix this? Do I need to add a default constructor to my base class? Or do I need to add a constructor to my mock class? Or something else?

推荐答案

您可以在您的mock中添加一个构造函数,委托给Employee构造函数:

You can just add a constructor to your mock that delegates to the Employee constructor:

 class MockEmployee : public Employee {
     public:
         MockEmployee(PensionPlan* pension_plan, const char* full_name)
         : Employee(pension_plan, full_name) {}
         // ...
 };

然后像构建Employee一样构造MockEmployee。但是,有一些事情可以改进关于这个代码,我强烈推荐,这将简化这:

Then construct MockEmployee like you would construct Employee. However, there are a couple things that can be improved about this code that I would highly recommend and that would simplify this:


  1. 使员工纯

  2. 将当前Employee实现重命名为描述员工类型的名称(例如FullTimeEmployee或EmployeeWithPensionPlan),并使其从纯虚拟类型继承。

  3. 使用virtual〜Employee()而不是virtual〜Employee(void)(在参数中明确使用void是C语言的保留,在大多数C ++

  4. 使用const string&

  1. Make Employee pure virtual (with a protected default constructor).
  2. Rename the current Employee implementation to a name that describes the kind of employee (e.g. FullTimeEmployee or EmployeeWithPensionPlan) and make it inherit from the pure virtual type.
  3. Use "virtual ~Employee()" instead of "virtual ~Employee(void)" (using void explicitly in the parameter is a hold over from C and is out of vogue in most C++ communities as far as I'm aware).
  4. Use "const string&" instead of "const char*" for the name.

因此,为了澄清,我的建议是:

So, to clarify, my recommendation would be:

class Employee {
    public:
        virtual ~Employee() {}
        virtual double GetSalary() const = 0;
    protected:
        Employee() {}
};

class FullTimeEmployee : public Employee {
     // your concrete implementation goes here
};

class MockEmployee : public Employee {
    public:
        MockEmployee() {}
        virtual ~MockEmployee() {}
        // ... your mock method goes here ...
};

这篇关于Google Mock:“没有适当的默认构造函数可用”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆