인터페이스와 함께 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 ++의 출력을 추가했습니다. –