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

ここで何が起こっているのか誰か説明してもらえますか?


クラス:

ExecutionStrategyInterface.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_

TaskCollectionInterface.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_

ExecutorInterface.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_

TaskExecutor.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_

TaskExecutor.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

名前空間の外部でクラスを前方宣言しているため、これは混乱を招きます。そのため、同じ名前の2つの異なるクラスになってしまいます。代わりに、次のようなものが必要になります。

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

現在のように、executeメソッドは、mylib :: core::TaskCollectionInterfaceではなく::TaskCollectionInterfaceへのポインターを取得しています。

于 2012-04-11T05:46:23.587 に答える
0

gccが言うときtype&、それはあなたが左辺値を渡していると言うことの省略形であり、非定数参照をとる関数が実行可能な候補であることを知っています。

あなたが持っている問題は、あなたがメソッドをとることとして宣言した::TaskCollectionInterfaceが、エラーメッセージはあなたがを渡そうとしていることを示しているということです::mylib::core::TaskCollectionInterface

名前空間での宣言をマスクする::mylib::core::TaskCollectionInterfaceinの宣言があります。TaskCollectionInterface.h::TaskCollectionInterfacemylib::core

于 2012-04-11T05:59:27.147 に答える
-1

これは、メソッドが参照を必要としているときTaskCollectionInterface* tciに、ExecutionStrategyInterface::executeメソッドへのポインターを渡しているためです。したがって、そのポインタをその関数に渡すときに、そのポインタを逆参照する必要があります。

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