简体   繁体   中英

Unused variables & Primary-Expression expected before ']'

I looked at the other answers, but it didn't really help since 1) I don't understand it (especially if it's not exactly my problem) and 2) I noticed that the way my class is learning how to program is vastly different from what I've seen online. For example, we are using std. So I'm sorry for the repetitive question.

Here are my compiler errors: main.cc: In function '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();
         ^

Here's my code: It was actually pre-made by our teacher; our main focus was the functions and how to sum up C style and C++ style arrays/vectors. I did my code exactly like my friend's but for some reason mine won't compile.

Please and thank you for all the help.

#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";
    }
}                

Presumably, the following is a typo:

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

The compiler actually pointed you to exactly this code, so you could have spotted this. Or, at least, compared this part of the code with your friend's version (which cannot be "exactly like" yours if it compiles).

The warnings are just that: warnings. And they're warning you about what they say they're warning you about: variables that you declare but never use. So why declare them?

Finally, I don't understand why you say you're not "using std" — assuming you are not referring to the using namespace statement (because you are using that), I can only presume you think you're not using the standard library. Yet, you are! All over the place, in fact! Vectors, C++11 arrays, streams…

The compiler pointed you to the obvious errors:

  • arr[i, size_t] should be arr[i] .

  • The first function loops ARR_SIZE times instead of using the size argument.

  • The second function loops ARR_SIZE times instead of using the size variable.
  • The third function loops ARR_SIZE times also, instead of using size computed as vec.size() .

All three functions might produce the correct sum, but only by coincidence.

Here is a corrected version:

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 < size; i++)
        sum += arr[i];
    return sum;
}

int sum_up(array<int, ARR_SIZE> arr) {
    int sum = 0;
    for (size_t i = 0, size = arr.size(); i < size; i++)
        sum += arr[i];
    return sum;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM