小编Ant*_*cci的帖子

为什么 const 阻止编译?

我不确定如何在不创建额外变量的情况下解决此问题。这是无法编译的代码:

std::string & printVec(std::vector<double> const &ds, std::string &dum) {
    std::vector<double>::iterator it;
//    std::vector<double> dsi = ds; // Created just to get the code to work, using dsi.begin() etc. 
    dum = " ";
    for (it = ds.begin(); it != ds.end(); it++) { // Compiler error. No suitable "="
        dum = dum + std::to_string(*it) + " ";
    }
    return dum;
}
Run Code Online (Sandbox Code Playgroud)

如果我删除输入上的 const,它会编译:

std::string & printVec(std::vector<double> &ds, std::string &dum) {
    std::vector<double>::iterator it;
    dum = " ";
    for (it = ds.begin(); it != …
Run Code Online (Sandbox Code Playgroud)

c++ constants

2
推荐指数
1
解决办法
106
查看次数

标签 统计

c++ ×1

constants ×1