如何将Child类的向量传递到需要Parent类向量的函数中? [英] How to pass a vector of a Child class in to a function expecting a vector of Parent class?

查看:57
本文介绍了如何将Child类的向量传递到需要Parent类向量的函数中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将Child传递给期望有Parent的成员函数,但是当使用向量时,出现编译错误,指出没有匹配的声明.请参阅底部对getUniqueLabels()的CorrelationEngineManager.cpp调用

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

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

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

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

推荐答案

在C ++中这是不可能的,这需要称为协方差的功能.

This is not possible in C++, this requires a feature called covariance.

即使类型 A 是类型 B 的子类,类型 X< A> 与类型完全无关> X< B>

Even if type A is a subclass of type B, type X<A> is completely unrelated to type X<B>

因此,您不能将 std :: vector< UPSEvent> 传递给需要 std :: vector< Event> 的函数,因为它们是不相关的类型.即使通过引用/指针传递也不起作用.

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.

一种方法是使两个向量保持指向 Event 的指针,然后它们将具有相同的类型.

One would be to make both vectors hold pointers to Event, then they would have identical types.

另一种方法是按照丹尼尔(Daniel)的建议将函数设为模板函数.

The other, would be to make the function a template function, as Daniel suggests.

正如Billz指出的那样,您还需要修复签名.

You would need to fix the signature as well, as billz points out.

这篇关于如何将Child类的向量传递到需要Parent类向量的函数中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆