简体   繁体   English

使用引用在对象内包含对象向量

[英]Including a vector of Objects within an Object, using reference

I am trying to establish the relationship between two different classes with my code. 我试图用我的代码建立两个不同类之间的关系。 I have a class Object and a Class Interface. 我有一个类对象和一个类接口。 The Object class can hold any number of Interfaces, of which I have chosen to store within a vector of unique pointers. Object类可以容纳任意数量的接口,我已经选择将其存储在唯一指针的向量内。

My Object class definition is: 我的Object类定义是:

#ifndef BASE_OBJECT
#define BASE_OBJECT

#include <vector>
#include <memory>

#include "./Interface.hxx"

class Object
{
/*****************
    Properties
******************/
//General
std::string ObjectType;

//Interfaces - All objects have at least one sender or receiver
//(otherwise it does nothing)
//Senders
typedef std::vector<std::unique_ptr<Interface>> Senders;

//Receivers
typedef std::vector<std::unique_ptr<Interface>> Receivers;

/**********************
    Member functions
**********************/
public:
    //Standard constructor
    Object(void);

    //Copy constructor
    Object(const Object& from);

    //Virtual destructor
    virtual ~Object(void);

    //Add an interface
    void add_interface(std::string type);
};

#endif

I haven't included my interface class, as it doesn't define anything as yet. 我还没有包含我的接口类,因为它还没有定义任何东西。 When trying to add an interface to my Object, I call the following routine from my main function: 当尝试向我的对象添加接口时,我从主函数调用以下例程:

//Add interface to object
MyObject->add_interface("Sender");

Which calls the routine add_interface: 其中调用例程add_interface:

//Add an interface to the object
void Object::add_interface(std::string type)
{
if (type.compare("Sender")){
    auto intf = std::unique_ptr<Interface>(new Interface);
    this->Senders.push_back(std::move(intf));
}
else if (type.compare("Receiver")){
    auto intf = std::unique_ptr<Interface>(new Interface);
    this->Receivers.push_back(std::move(intf));
}
else{
    //Throw exception
}
}

Currently within visual studio express, the "Senders" and "Receivers" is highlighted with the warning "Error: type name is not allowed", and upon compilation I get the following error: 当前在Visual Studio Express中,“发送方”和“接收方”以警告“错误:不允许输入类型名称”突出显示,并且在编译时出现以下错误:

'function-style cast': illegal as right side of -> operator. '函数样式转换':在->运算符的右侧无效。

I am new to smart pointers and OOP, so I am not really sure what I am doing wrong here. 我是智能指针和OOP的新手,所以我不确定自己在做什么错。 Can anyone please explain my mistake? 谁能解释我的错误? Many thanks in advance. 提前谢谢了。

Senders and Receivers in your code are just types, not fields. 您代码中的发送者和接收者只是类型,而不是字段。 You should declare two fields, eg: Senders senders; 您应该声明两个字段,例如:发件人发件人; Receiers receivers 接收器接收器

Senders and Receivers are typedef s within your Object class, not members. SendersReceiversObject类中的typedef ,而不是成员。

Objects (or variables) and types cannot generally have the same name, in the same scope 对象(或变量)和类型通常不能在相同范围内具有相同的名称

You have: 你有:

typedef std::vector<std::unique_ptr<Interface>> Senders;
typedef std::vector<std::unique_ptr<Interface>> Receivers;

Senders and Receivers are types, indeed. 实际上, SendersReceivers是类型。 They are not objects. 它们不是对象。 Hence, this->Senders and this->Receivers does not make sense. 因此, this->Sendersthis->Receivers没有意义。

You need to use: 您需要使用:

Senders senders;     // A variable that can hold senders
Receivers receivers; // A variable that can hold receivers.

And then use: 然后使用:

this->senders.push_back(std::move(intf));

and

this->receivers.push_back(std::move(intf));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM