未使用的变量&在']'之前预期的主要表达

时间:2016-03-12 23:19:19

标签: c++ c++11

我查看了其他答案,但它并没有真正帮助,因为1)我不理解它(特别是如果它不是我的问题)和2)我注意到了我班上学习如何编程的方式与我在网上看到的大不相同。例如,我们正在使用std。所以我很抱歉重复的问题。

以下是我的编译器错误:     main.cc:在函数'int sum_up(std :: array)'中:

main.cc:21:19: error: expected primary-expression before ‘]’ token
     sum+=arr[i,size_t];
               ^

main.cc:18:9: warning: unused variable ‘size’ [-Wunused-variable]
  size_t size = arr.size(); //A size_t is just an unsigned int on this system
         ^

main.cc: In function ‘int sum_up(std::vector<int>)’:

main.cc:28:9: warning: unused variable ‘size’ [-Wunused-variable]
  size_t size = vec.size();
         ^

这是我的代码: 它实际上是由我们的老师预先制作的;我们主要关注的是函数以及如何总结C风格和C ++风格的数组/向量。我的代码与我的朋友完全一样,但出于某种原因我的编译没有。

请感谢您的帮助。

#include <iostream>
#include <cstdlib>
#include <array>
#include <vector>

using namespace std;

const size_t ARR_SIZE = 5;

int sum_up(int arr[], size_t size) { //Function to sum up a C-style array
    int sum = 0;
    for (size_t i = 0; i < ARR_SIZE; i++)
        sum += arr[i];
    return sum; //Stub
}

int sum_up(array<int, ARR_SIZE> arr) {
    size_t size = arr.size(); //A size_t is just an unsigned in 
    //on       this system
    int sum = 0;
    for (size_t i = 0; i < ARR_SIZE; i++)
        sum += arr[i, size_t];

    return sum; //Stub 
}

int sum_up(vector<int> vec) {
      size_t size = vec.size();
      int sum = 0;
      for (size_t i = 0; i < ARR_SIZE; i++)
          sum += vec[i];

      return sum; //Stub
}

void die() {
     cout << "Invalid input.\n";
     exit(EXIT_FAILURE);
}

int main() {
    int c_arr[ARR_SIZE] = {}; //Size ARR_SIZE, initialized to zero
    array<int, ARR_SIZE> cpp_arr = {}; //Size ARR_SIZE, initialized to zero
    vector<int> vec_arr(ARR_SIZE);  //Size ARR_SIZE, initialized to zero

    cout << "Welcome to the Arrayitizer 2000(tm). Today we will be doing    arrays three different ways.\n";
    cout << "Please enter " << ARR_SIZE << " integers.\n";

    //Now let's read into three arrays
    for (size_t i = 0; i < ARR_SIZE; i++) {
        int x;
        cin >> x;
        if (!cin) die();
        c_arr[i] = x;
        cpp_arr[i] = x;
        vec_arr.at(i) = x;
    }

    //Let's print them out
    cout << "You entered (one column for each of the three arrays):\n";
    for (size_t i = 0; i < ARR_SIZE; i++) {
        cout << c_arr[i] << "\t" << cpp_arr[i] << "\t" << vec_arr.at(i) << endl;
    }

    cout << "Summming up the three arrays:\n";

    //Now lets sum them up and verify they all return the same value
    int sum_c =   sum_up(c_arr, ARR_SIZE);
    int sum_cpp = sum_up(cpp_arr);
    int sum_vec = sum_up(vec_arr);

    cout << "sum_c:   " << sum_c   << endl;
    cout << "sum_cpp: " << sum_cpp << endl;
    cout << "sum_vec: " << sum_vec << endl;

    if (sum_c == sum_cpp and sum_c == sum_vec) {
        cout << "Congrats, you added up all three arrays the same!\n";
    } else {
        cout << "Unfortunately, your code for summing up the three arrays returned different results.\n";
    }
}                

2 个答案:

答案 0 :(得分:3)

据推测,以下是拼写错误:

arr[i,size_t]
//   ^^^^^^^

编译器实际上向您指出了这段代码,所以您可能已经发现了这一点。或者,至少将代码的这一部分与你朋友的版本进行比较(如果它编译的话,不能和#34;完全相同的话。)

警告就是:警告。并且他们会警告你他们所说的他们警告你:你声明但从不使用的变量。那么为什么要申报呢?

最后,我不明白为什么你说你没有&#34;使用std&#34; - 假设您没有引用using namespace语句(因为 使用它),我只能假设您认为您没有使用标准库。但是,你是!事实上,到处都是!向量,C ++ 11数组,流......

答案 1 :(得分:0)

编译器向您指出了明显的错误:

  • arr[i, size_t]应为arr[i]

  • 第一个函数循环ARR_SIZE次,而不是使用size参数。

  • 第二个函数循环ARR_SIZE次,而不是使用size变量。
  • 第三个函数也循环ARR_SIZE次,而不是使用计算为size的{​​{1}}。

所有三个函数都可能产生正确的总和,但只能巧合。

以下是更正后的版本:

vec.size()