私はインターフェイスでC++イテレータを使用しようとしていますが、動作させることはできません。C++イテレータ、インターフェイス、ポインタ
私は、どのようなタイプでベクターの内容を選択するのか少し失われています。これはポインタである必要がありますか?私は新しい実装()をしなければならないのですか?要するに、それは私には不明であり、私はそれに関する有用な事例を見つけることができません。
ここには、インターフェイスと実装(.hファイル)があります。
class Interface{
public:
virtual int method() = 0;
};
class Implementation1 : public Interface{
public:
int method();
};
class Implementation2 : public Interface{
public:
int method();
};
.cppファイル:
#include "content.h"
int Implementation1::method(){
return 1;
}
int Implementation2::method(){
return 2;
}
そして、私の主な機能:
#include "content.h"
#include <vector>
#include <iostream>
using namespace std;
int main(void)
{
// create the vector and put elements in it
vector<Interface*> elements;
elements.push_back(new Implementation1());
elements.push_back(new Implementation1());
elements.push_back(new Implementation2());
// now iterate on them
vector<Interface*>::iterator iterator;
for(iterator = elements.begin(); iterator != elements.end(); ++iterator){
*iterator->method();
}
return 1;
}
compilatorが出力している:私は何について
main.cpp: In function ‘int main()’: main.cpp:19: error: request for member ‘method’ in ‘* iterator.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> with _Iterator = Interface**, _Container = std::vector >’, which is of non-class type ‘Interface*’
任意のアイデアここで間違っている?
「働いていない」とはどういう意味ですか? – kennytm
コンパイルに失敗しました。私はg ++の出力を追加しました。 –