0

我有 5 个文件:

  • ExecutionStrategyInterface.h
  • ExecutorInterface.h
  • TaskCollectionInterface.h
  • TaskExecutor.h
  • TaskExecutor.cpp.

TaskExecutor实现以下成员方法:

void TaskExecutor::execute(TaskCollectionInterface* tci, const ExecutionStrategyInterface& es) {
    es.execute(tci);
}

在编译时,编译器调用一个成员方法,其参数类型为指向引用的指针
(即:)mylib::core::TaskCollectionInterface*&

TaskExecutor.cpp: In member function ‘virtual void mylib::core::TaskExecutor::execute(mylib::core::TaskCollectionInterface*, const mylib::core::ExecutionStrategyInterface&)’:
TaskExecutor.cpp:16: error: no matching function for call to ‘mylib::core::ExecutionStrategyInterface::execute(mylib::core::TaskCollectionInterface*&) const’
./././ExecutionStrategyInterface.h:24: note: candidates are: virtual void mylib::core::ExecutionStrategyInterface::execute(TaskCollectionInterface*) const
make: *** [TaskExecutor.o] Error 1

谁能解释一下这里发生了什么?


课程:

执行策略接口.h

#ifndef _EXECUTIONSTRATEGYINTERFACE_H_
#define _EXECUTIONSTRATEGYINTERFACE_H_

class TaskCollectionInterface;

namespace mylib { namespace core {

/**
 *  Interface for executing a strategy.
 */
class ExecutionStrategyInterface {
 public:
    /**
     * Executes a strategy
     */
    virtual void execute(TaskCollectionInterface* tci) const = 0;
};

}} // namespaces

#endif // _EXECUTIONSTRATEGYINTERFACE_H_

任务集合接口.h

#ifndef _TASKCOLLECTIONINTERFACE_H_
#define _TASKCOLLECTIONINTERFACE_H_

#include "./ExecutionStrategyInterface.h"

namespace mylib { namespace core {

/**
 *  Interface for a collection of tasks.
 */
class TaskCollectionInterface {
 public:
    ~TaskCollectionInterface();
};

}} // namespaces

#endif // _TASKCOLLECTIONINTERFACE_H_

执行器接口.h

#ifndef _EXECUTORINTERFACE_H_
#define _EXECUTORINTERFACE_H_

class ExecutionStrategyInterface;
class TaskCollectionInterface;

#include "./ExecutionStrategyInterface.h"
#include "./TaskCollectionInterface.h"

namespace mylib { namespace core {

/**
 *  Interface for an executor.
 */
class ExecutorInterface {
 public:
    virtual void execute(TaskCollectionInterface* tci, const ExecutionStrategyInterface& es) = 0;
    ~ExecutorInterface();
};

}} // namespaces

#endif // _EXECUTORINTERFACE_H_

任务执行器.h

#ifndef _TASKEXECUTOR_H_
#define _TASKEXECUTOR_H_

#include "./ExecutorInterface.h"

class TaskCollectionInterface;
class ExecutionStrategyInterface;

namespace mylib { namespace core {

/**
 *  Task Runner.
 */
class TaskExecutor: public ExecutorInterface {
 public:
    virtual void execute(TaskCollectionInterface* tci, const ExecutionStrategyInterface& es) = 0;
};

}} // namespaces

#endif // _TASKEXECUTOR_H_

任务执行器.cpp

#include "./TaskExecutor.h"
#include "./ExecutionStrategyInterface.h"
#include "./TaskCollectionInterface.h"

namespace mylib { namespace core {

void TaskExecutor::execute(TaskCollectionInterface* tci, const ExecutionStrategyInterface& es) {
    es.execute(tci);
}

}} // namespaces
4

3 回答 3

2

这很令人困惑,因为您在命名空间之外前向声明该类,因此您最终会得到两个具有相同名称的不同类。你会想要这样的东西:

namespace mylib {
  namespace core {
    class TaskCollectionInterface;
    class ExecutionStrategyInterface {
      .
      .
      .
    };
  }
}

您现在拥有的方式是,您的执行方法采用指向 ::TaskCollectionInterface 而不是 mylib::core::TaskCollectionInterface 的指针。

于 2012-04-11T05:46:23.587 回答
0

当 gcc 说type&时,它只是表示您正在传递一个左值的简写,以便您知道采用非常量引用的函数是可行的候选者。

您遇到的问题是您已将该方法声明为采用 a ::TaskCollectionInterface,但错误消息表明您正在尝试传递 a ::mylib::core::TaskCollectionInterface

您有一个 in 的声明,::mylib::core::TaskCollectionInterfaceTaskCollectionInterface.h掩盖::TaskCollectionInterface了 namespace 中的声明mylib::core

于 2012-04-11T05:59:27.147 回答
-1

这是因为您正在传递一个指向TaskCollectionInterface* tciExecutionStrategyInterface::execute方法的指针,而它需要一个引用。因此,在将指针传递给该函数时,您必须取消引用该指针:

void TaskExecutor::execute(TaskCollectionInterface* tci, const ExecutionStrategyInterface& es) {
    es.execute(*tci);
}
于 2012-04-11T05:51:28.040 回答