baozhadehulu 2015-06-03 14:27 采纳率: 52.6%
浏览 5589
已采纳

关于C++ vector 的问题

请问下列代码错在哪里?
#include<iostream>
#include<vector>
#include<string>
using std::vector;
using namespace std;
int main()
{
    int word;
    vector<int> var;
    vector<int> res_var;
    cout << "Please enter a group numbers:" << endl;
    while (cin >> word){
        var.push_back(word);
        for (vector<int>::size_type i = 0; i != (var.size()-1); i = i + 2){
            res_var = var[i] + var[i+1];
            cout << res_var << " ; " ;
        }
        if (var.size() % 2 = 1)
            cout << "The last one no add."  << endl;
    }
    return 0;
}
 错误信息:
 [Error] no match for 'operator=' (operand types are 'std::vector<int>' and 'int')
 [Error] no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::vector<int>')
  • 写回答

7条回答 默认 最新

  • 纵横车 2015-06-03 14:37
    关注

    创建vector的时候初始化大小,res_var后面加下标,最后的if里写“==”。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?