小编Hid*_*eld的帖子

附加到联合向量

我有一个工会,定义如下:

union CellType {
    std::string str;
    int i;
    double d;
    Money<2> m;  // Custom class for fixed-decimal math.
};
Run Code Online (Sandbox Code Playgroud)

然后,我就有了这样的联合的向量。

std::vector<CellType> csvLine;
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何将值附加到向量的末尾?我通常使用字符串、整数等的push_backfor vector,但是当向量元素是并集时无法完全弄清楚语法。任何帮助,将不胜感激。

(是的,这个类一次读取一行 CSV 文件,如果这有什么区别的话。)

这是失败的 MRE

#include <vector>
#include <iostream>


union CellType {
    std::string str;
    int i;
    double d;
//    Money<2> m;  // Custom class for fixed-decimal math.
};
int main() {
    std::vector<CellType> csvLine;
    CellType c;
    c.str = "foo";
    csvLine.push_back(c);
}
Run Code Online (Sandbox Code Playgroud)

Money 被注释掉是因为与 MRE 无关

错误

严重性代码说明项目文件行抑制状态错误 C2280 'CellType::CellType(void)':尝试引用已删除的函数 ConsoleApplication1 C:\work\ConsoleApplication1\ConsoleApplication1.cpp 22

c++ stdvector unions

4
推荐指数
1
解决办法
1767
查看次数

标签 统计

c++ ×1

stdvector ×1

unions ×1