2013-02-21 2 views
2

부모를 기대하는 멤버 함수에 Child를 전달할 수 있지만 벡터를 사용할 때 일치하는 선언이 없다는 컴파일 오류가 발생합니다. 바닥Child 클래스의 벡터를 Parent 클래스의 벡터를 기대하는 함수로 전달하는 방법은 무엇입니까?

ServerEvent.h

#ifndef SERVEREVENT_H 
#define SERVEREVENT_H 

#define SERVEREVENT_COLS 3 

#include "Event.h" 
#include <vector> 


class ServerEvent: public Event { 
private: 

public: 
    ServerEvent(std::vector<std::string> tokens); 
    void print(); 
}; 

#endif 

Event.h

#ifndef EVENT_H 
#define EVENT_H 

#include <string> 

#define EVENT_STOP 0 
#define EVENT_START 1 

class Event { 
private: 

protected: 
    double time; 
    std::string label; 
    int type; // EVENT_START OR EVENT_STOP 

public: 

}; 

#endif 

CorrelationEngineManager.h

class CorrelationEngineManager { 
private: 
    std::vector<ServerEvent> s_events; 
    std::vector<UPSEvent> u_events; 
    std::vector<TimeRecord> s_timeRecords; 
    std::vector<TimeRecord> u_timeRecords; 
    // typeOfEvent gets type of event, 0 for error, look at #defines for codes 
    int typeOfEvent(std::vector<std::string>); 
    int createTimeRecords(); 
    std::vector<std::string> getUniqueLabels(std::vector<Event> events); 


public: 
    CorrelationEngineManager(); 
    //~CorrelationEngineManager(); 
    int addEvent(std::vector<std::string> tokens); //add event given tokens 
    void print_events(); 
}; 

CorrelationEngineManager.cpp에서 getUniqueLabels()에 CorrelationEngineManager.cpp 호출을 참조하십시오

int CorrelationEngineManager::createTimeRecords() { 
    std::vector<std::string> u_sLabels; // unique server labels 
    std::vector<std::string> u_uLabels; // unique UPS labels 
    u_sLabels = getUniqueLabels(s_events); 
// u_uLabels = getUniqueLabels(u_events); 
    return 1; 
} 
// returns a vector of unique labels, input a vector of events 
std::vector<std::string> CorrelationEngineManager::getUniqueLabels(std::vector<Event> events) { 

    std::vector<std::string> temp; 
    return temp; 
} 

컴파일 오류가

CorrelationEngineManager.cpp: In member function ‘int CorrelationEngineManager::createTimeRecords()’: 
CorrelationEngineManager.cpp:60: error: no matching function for call 
to ‘CorrelationEngineManager::getUniqueLabels(std::vector<ServerEvent, 
std::allocator<ServerEvent> >&)’ CorrelationEngineManager.h:23: note: 
candidates are: std::vector<std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, 
std::allocator<std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > > > 
CorrelationEngineManager::getUniqueLabels(std::vector<Event, 
std::allocator<Event> >) make: *** [CorrelationEngineManager.o] Error 1 
+0

가능한 중복 (HTTP : //stackoverflow.com/questions/2376334/c-how-do-i-pass-a-container-of-derived-classes-to-a-function-expecting-a-cont) – jogojapan

답변

3

이 C++에 불가능이 공분산라는 기능이 필요합니다. 그들은 관련이없는 유형이 때문에 유형 A 유형 B, 유형 X<A>의 서브 클래스 인 경우에도

X<B>은 따라서 당신이 std::vector<Event>을 기대하는 기능에 std::vector<UPSEvent>를 전달할 수 없습니다 입력하는 것이 전혀 관계가없는 것입니다. 참조/포인터를 통한 전달조차도 작동하지 않습니다.

이 문제를 해결하는 방법에는 두 가지가 있습니다.

두 벡터를 모두 Event에 대한 포인터로 유지하는 것은 동일한 유형을 갖게됩니다.

다른 기능은 템플릿 기능인데, 다니엘이 제안한 것입니다.

bill30이 가리키는대로도 마찬가지로 으로 수정해야합니다.

3

기능 템플릿 기능으로 변경 될 수 있습니다 :

template< typename T > 
std::vector<std::string> getUniqueLabels(std::vector<T> events); 
[C++? : 나는 그들의 기본 클래스의 컨테이너를 기대 함수에 파생 클래스의 컨테이너를 통과하려면 어떻게]의
+0

이벤트를 호출해야하는 경우 멤버 함수 (부모 클래스와 자식 클래스 공통) 어떻게 할 수 있습니까? –

+0

@Rorschach 나는이 질문을 이해하고 있는지 확신 할 수 없다. 예를 들어'for (const auto & e : events) e.some_func(); '와 같이 호출하면됩니다. –

+0

죄송하지만 어쩌면 내가 분명하지 않았습니다. 기본적으로 템플릿을 사용하여 부모 클래스의 벡터 또는 자식 클래스의 벡터를 매개 변수로 받아들이는 템플릿 함수를 만들었습니다. 예를 들어 events.at (idx) .any_function()과 같이 벡터의 한 요소에 대한 멤버 함수를 호출하려고한다고 가정합니다. any_function()은 부모 클래스와 상속 된 자식 클래스의 함수입니다. 희망은 조금 더 명확하게되었습니다 –

관련 문제