【问题标题】:Is there any way to return three containers in one getter?有没有办法在一个吸气剂中返回三个容器?
【发布时间】:2016-12-31 16:13:07
【问题描述】:

我想知道有什么方法可以同时访问三个容器。 我有这样的课:

class DataContainer
{

private:
    std::vector<Rental*> rentals;
    std::vector<Vehicle*> vehicles;
    std::vector<Client*> clients;

public:
    DataContainer();
    bool loadObjects();
    bool createRentals();
    std::string showVehicles() const;
    std::string showClients() const;
    std::string showDetails() const ;
    std::tuple< std::vector<Vehicle*>type1, std::vector<Client*>type2, std::vector<Rental*>type3 > getContainers();
    virtual ~DataContainer();

};

我希望有可能从其他类访问这些容器,这就是为什么我想设置一些吸气剂,但我的问题来了。我不知道我是否做错了什么,但我得到的错误是:

include\DataContainer.h|74|error: template argument 1 is invalid|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 1 second(s)) ===|

我的函数如下所示:

std::tuple< std::vector<Vehicle*>type1, std::vector<Client*>type2, std::vector<Rental*>type3 > DataContainer::getContainers()
{
   return std::make_tuple(vehicles,clients,rentals);
}   

希望有人能给我一些提示,如果这对我正在开发 C++11 有任何影响。

【问题讨论】:

  • std::tuple&lt;T1, T2, T3&gt; 期望 T1, T2, T3 是类型。 std::vector&lt;Vehicle*&gt;type1 不是有效类型。 std::vector&lt;Vehicle*&gt; 是。
  • type2 等不应出现在 std::tuple&lt; std::vector&lt;Vehicle*&gt;type1, std::vector&lt;Client*&gt;type2, std::vector&lt;Rental*&gt;type3 &gt; getContainers(); 行中。然而,这看起来就像是一开始就相当糟糕的类设计。
  • 即使错误得到修复,您最终也可能会复制 很多 指针以返回三个向量。你想达到什么目的?

标签: c++ c++11 tuples getter


【解决方案1】:

类型应该是

std::tuple< std::vector<Vehicle*>, std::vector<Client*>, std::vector<Rental*>>

字段名无效。

作为元组的替代方案,您可以定义一个包含 3 个容器的结构类型并将其返回。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 2021-06-01
    • 2017-01-02
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多