-3

I'm getting an error on the following code snippet;


#include <cstdio>
#include <cstdlib>
#include <vector>
#include <iostream>

using namespace std;

struct tuple {
    int x;
    int y;
};

int main() {

    //srand(time(NULL));
    vector<tuple> locations;

    int dimentions = 20;
    double filledness = 0.65;


    while (locations.size() < dimentions * dimentions * filledness) {
        tuple point;
        point.x = rand() % dimentions;
        point.y = rand() % dimentions;

        locations.push_back(point);
    }

    int count = locations.size();
    tuple start, end;

    start = locations[rand() % count];
    end = locations[rand() % count];

    cout << count << endl;

    for (int i = 0; i < locations.size(); i++) {
        cout << locations[i].x << " " << locations[i].y << endl;
    }
    cout << start.x << start.y << endl;
    cout << end.x << end.y;

    return 0;
}

The vector initialization is in my main and the struct outline just above. I obviously don't understand something about vectors. I've tried making the struct a class as well as replacing vector of tuples with a vector of tuple*.

Can someone please explain why a vector can't be used in this way,

I would prefer if you didn't tell me how to fix the error directly.

error: template argument 1 is invalid
     vector<tuple> locations;
  • There's nothing obviously wrong with the shown code. Therefore, the problem must be in the code that's not shown. You must follow the instructions for creating a [mcve], if you want someone to help you. – Sam Varshavchik Jan 27 '19 at 01:50
  • In C++ a struct/class declaration requires a semi-colon ; after. I don't see any in your code? I don't think this is your full code. – Jack Jan 27 '19 at 01:50
  • The code above is perfectly valid. Make sure you call it an `std::vector` and `#include `. – Itay Grudev Jan 27 '19 at 01:54
  • Also post the exact error. – Itay Grudev Jan 27 '19 at 01:55
  • posted the exact error –  Jan 27 '19 at 02:04
  • 1
    @SwimMaster Do not call your type `tuple`, and especially after you used `using namespace std;`. There is a `std::tuple` in the C++ standard library. – PaulMcKenzie Jan 27 '19 at 02:06
  • 1
    You've just discovered [why `using namespace std;` is bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Your code is confused between your `tuple` and `std::tuple`. Do yourself a big favor, and completely forget that `using namespace std;` is a part of the C++ language. Get rid of it, or perpetually run into mysterious compilation errors, like this one. – Sam Varshavchik Jan 27 '19 at 02:12

1 Answers1

1

The reason for your error is this struct:

struct tuple {
    int x;
    int y;
};

1) You called your type tuple, when there is a std::tuple that exists in the standard library and

2) You used using namespace std;, thus any mention of tuple in your code potentially clashes with std::tuple.

If you rename your struct to my_tuple or similar, the error that you are getting now should be remediated.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45