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

关于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条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容