Google Mock:“没有合适的默认构造函数可用”?

发布于 2024-11-18 18:17:26 字数 719 浏览 3 评论 0原文

将 Visual Studio 2010 C++ 与 googlemock 结合使用。我正在尝试使用我创建的模拟,但出现编译器错误:

EmployeeFake employeeStub;

错误是:

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

EmployeeFake:

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

Employee:

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

    virtual double GetSalary() const;
}

我认为问题是基类没有默认构造函数,但应该如何我修好这个吗?我需要向我的基类添加默认构造函数吗?或者我需要向我的模拟类添加一个构造函数吗?还是别的什么?

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;

The error is:

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

EmployeeFake:

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

Employee:

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

    virtual double GetSalary() const;
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-11-25 18:17:26

您只需将一个构造函数添加到您的模拟中,该构造函数委托给 Employee 构造函数:

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

然后像构造 Employee 一样构造 MockEmployee。然而,我强烈建议您对这段代码进行一些改进,这将简化这一过程:

  1. 使 Employee 成为纯虚拟的(使用受保护的默认构造函数)。
  2. 将当前的 Employee 实现重命名为描述员工类型的名称(例如 FullTimeEmployee 或 EmployeeWithPensionPlan),并使其继承自纯虚拟类型。
  3. 使用“virtual ~Employee()”而不是“virtual ~Employee(void)”(在参数中显式使用 void 是 C 的保留,据我所知,在大多数 C++ 社区中已经不再流行)。
  4. 使用“const string&”而不是“const char*”作为名称。

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

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

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) {}
         // ...
 };

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. 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 ...
};
梦亿 2024-11-25 18:17:26

您已经提出了一个可能的答案,但让我们阐明一些选项:

1)使基础默认可构造。最简单的方法是提供默认参数:(

explicit Employee(PensionPlan *pensionPlan = 0, const char * fullName = "");

请注意,我们说“显式”以避免来自 PensionPlan* 的默认转换。)

2) 在派生类的构造函数的基初始化列表中调用构造函数:

EmployeeFake::EmployeFake() : Employee(0, "") { }

2a) 给 EmployeeFake 一个适当的构造函数并将其传递:(

EmployeeFake::EmployeeFake(PensionPlan *p) : Employee(p, "[fake]") { } 

请注意,(1) 是声明,而 (2) 和 (2a) 是定义。)

You've already suggested a possible answer, but let's spell out some options:

1) Make the base default-constructible. Easiest to do by providing default arguments:

explicit Employee(PensionPlan *pensionPlan = 0, const char * fullName = "");

(Note that we say explicit to avoid tacit conversions from PensionPlan*.)

2) Call the constructor in the derived class's constructor's base initializer list:

EmployeeFake::EmployeFake() : Employee(0, "") { }

2a) Give EmployeeFake an appropriate constructor and pass it on:

EmployeeFake::EmployeeFake(PensionPlan *p) : Employee(p, "[fake]") { } 

(Note that (1) is a declaration, while (2) and (2a) are the definitions.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文