问题
I can pass in a Child to a member function expecting a Parent, however when using vectors I get a compile error saying there's no matching declaration. See the CorrelationEngineManager.cpp call to getUniqueLabels() at the bottom
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
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;
}
compile error
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
回答1:
This is not possible in C++, this requires a feature called covariance.
Even if type A
is a subclass of type B
, type X<A>
is completely unrelated to type X<B>
Thus you cannot pass std::vector<UPSEvent>
to a function expecting std::vector<Event>
, since they are unrelated types. Even pass by reference/pointer will not work.
There are two ways to get around this.
One would be to make both vectors hold pointers to Event
, then they would have identical types.
The other, would be to make the function a template function, as Daniel suggests.
You would need to fix the signature as well, as billz points out.
回答2:
The function could be changed to a template function:
template< typename T >
std::vector<std::string> getUniqueLabels(std::vector<T> events);
来源:https://stackoverflow.com/questions/14992961/how-to-pass-a-vector-of-a-child-class-in-to-a-function-expecting-a-vector-of-par