0

I'm very new to programming and actively trying to teach myself C++. I'm trying to create a simple program that creates a vector, adds indexes in a for loop up to a value of 999, then reduces the vector back down to 0 indexes. My code is producing the VS error "too many arguments in function call". I've tried looking at other forum posts but I still can't quite understand what I'm doing wrong. My error exists in the second for loop at the line "index.pop_back(y);" here is my code, any additional coding tips help too, thanks.

My code:

#include <iostream>
#include <string>
#include <vector>

int main()
{
bool point_reached = 0;

std::vector<int> index = {};

for (int x = 0; x <= 999; x++)
{
    index.push_back(x);
    std::cout << index[x] << std::endl;

    if (x == 999)
    {
        point_reached = 1;
        std::cout << std::endl;
        std::cout << "point reached!" << std::endl;
    }
}

    for (int y = 1000; y > 0; y--)
    {
        index.pop_back(y);
        std::cout << index[y] << std::endl;
    }
}
  • 3
    [`pop_nack ()`](https://en.cppreference.com/w/cpp/container/vector/pop_back) doesn't have a parameter, or return value. To get the value just use [`back()`](https://en.cppreference.com/w/cpp/container/vector/back) before you pop. Refer to that reference before you ask here next time, please. – πάντα ῥεῖ Oct 01 '20 at 17:48
  • @πάνταῥεῖ, lol, `pop_nack()` removes all the fake elements of the vector fyi :) – anastaciu Oct 01 '20 at 17:53
  • 1
    Unrelated: `for (int x = 0; x <= 999; x++)` does the same number of iterations as `for (int y = 1000; y > 0; y--)`, but the `<= 999` and the `= 1000` is going to cause questions and make maintenance a bit more difficult. You don't want to use [magic numbers](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad), but if you do, use the same number. – user4581301 Oct 01 '20 at 17:54
  • Note: after the first loop, there are 1000 items in `index`, indexed 0 through 999. Accessing `index[1000]` before or after popping the last item from `index` is a bug. – user4581301 Oct 01 '20 at 18:23
  • Another minor code style remark: While `bool point_reached = 0;` is valid, C++ does have boolean literals and `bool point_reached = false;` (and, later, `point_reached = true;`) are a little clearer what's actually happening imo. (Not that you use that variable yet anyway, but as a general tip.) – Nathan Pierson Oct 01 '20 at 20:35

2 Answers2

0

index.pop_back(y); should be index.pop_back();. Not really clear to me why you had y there. The last element of a vector is just the last element of a vector, you don't need to add anything more.

john
  • 85,011
  • 4
  • 57
  • 81
0

You are passing y to index.pop_back but that function does not take parameters. If you are trying to pop the value off the stack and into y, try

y = index.back();
index.pop_back();
jkb
  • 2,376
  • 1
  • 9
  • 12
  • You probably wouldn't want `y = index.back()` if `y` is also the control variable of the loop. I might suggest just making the entire body of the second `for` loop `std::cout << index.pop_back() << std::endl;` – Nathan Pierson Oct 01 '20 at 20:37