【问题标题】:No Viable Overloaded +=?没有可行的重载+=?
【发布时间】:2014-09-27 02:43:48
【问题描述】:

有人可以告诉我为什么我会收到这个“没有可行的重载”吗?我很困惑为什么我会收到这个……我是新手。

int main()
{
char ch;
    vector<int> temp;

    ifstream infile;
    infile.open("tempsF.txt");

    if (infile.fail())
    {
        cout << "Could not open file numbers." << "\n";
        return 1;
    }
    int data;
    infile >> data;
    while (!infile.eof())
    {
        if(isalpha(ch) || ispunct(ch))
        {
            if(isupper(ch) && ch != '\n')

                temp += " ";<<<<<<<<<<<<<<<<<<<<<<<<< No Viable Overloaded '+='

                temp += ch;<<<<<<<<<<<<<<<<<<<<<<<<<< No Viable Overloaded '+='
        }
    }

【问题讨论】:

  • 您希望将字符串文字添加到整数向量中来做什么?
  • 错误信息很清楚。您正在使用vector&lt;int&gt;char[2] 类型的操作数执行+=。对于这些操作数类型,operator+= 没有重载,因此出现错误消息。

标签: c++ vector overloading


【解决方案1】:

不是你如何使用std::vector&lt;int&gt;。尝试更多类似的东西:

temp.push_back(42);

或者您可能想要std::vector&lt;std::string&gt;,那么您可以:

temp.push_back(" ");

但没有为std::vector 定义operator +=()

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2018-08-26
    • 2015-06-26
    • 1970-01-01
    • 2017-05-03
    相关资源
    最近更新 更多