1

I have a header file defining some parameters. I have defined some of the parameters as extern. My program works fine with other data types such as double and int, except when I try to add vector variables. The declaration in header file is

extern std::vector<double> my_vec;

In my main file, I am constructing the vector using this code:

std::vector<double> my_vec(3,0);

When I try to clear the vector using the clear method, the compiler is giving an error saying that unknown type. I am not even sure how to debug this. Can someone help?

P.S. I was originally trying to assign some values to this vector using:

my_vec[0] = 1;

but the compiler says that C++ requires a type specifier for all declarations. I googled this error, but I don't understand because I am specifying the type of my_vec.

Edit: example:

main.cpp
#include "params.h"
#include <vector>

std::vector<double> my_vec(3,0);

my_vec.clear();
// edit: my_vec[0] = 1; this also produces an error

int main(){
    return 0;
}

params.h
#include <vector>

extern std::vector<double> my_vec;

Error message:

main.cpp:6:1: error: unknown type name 'my_vec'
my_vec.clear();
^
main.cpp:6:7: error: cannot use dot operator on a type
my_vec.clear();
      ^
2 errors generated.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
Sahil Gupta
  • 103
  • 9

1 Answers1

7

You can't execute statements outside of a function - which is what you're trying to do with my_vec.clear();. It doesn't matter that clear() is a method of the vector class - invoking a method (as opposed to constructing a variable) is a statement, just like x = 1; . Those belong in functions.

You have to put your statement somewhere in your main(), e.g.:

int main(){
    my_vec.clear();
    return 0;
}

or make sure and construct my_vec the way you want it to look like, to begin with.

Also, more generally, you should avoid global variables if you don't really need them. And - you very rarely do. See:

Are global variables bad?

Edit: OP asks whether we can get around this restriction somehow. First - you really shouldn't (see what I just said). But it is possible: We can use a static block, which is implementable in C++, sort of.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Is this true with assignment as well? my_vec[0] = 1; doesn't seem to work; Error: C++ requires a type specifier for all declarations – Sahil Gupta Nov 09 '18 at 21:47
  • @SahilGupta: Yes. Outside of a function you can only _construct_ a global variable, not assign to an existing one. – einpoklum Nov 09 '18 at 21:49
  • 2
    @SahilGupta - that looks like an assignment but is in fact a function call disguised as an assignment. (int x =42 would be ok) – pm100 Nov 09 '18 at 21:49
  • @pm100: A function call is also generally not allowed, You meant to say, it's a _constructor_ invocation. – einpoklum Nov 09 '18 at 21:50
  • @einpoklum Right on! I was doing this for double variables and it was working. But when I tried on vectors the error was really weird. In the context of other answers, what you say makes complete sense. So, is there any other way around this? – Sahil Gupta Nov 09 '18 at 21:51
  • @SahilGupta: Well yes, there is. But I really want caution you _against_ trying to get around this. Anyway, since you asked - you can implement [static blocks in the global scope](https://stackoverflow.com/a/34321324/1593077) in C++ with some macro trickery. – einpoklum Nov 09 '18 at 22:24
  • @einpoklum `my_vec[0] = 1` is a call to `operator[]` not a contructor – pm100 Nov 09 '18 at 22:29
  • @pm100: I was talking about `x = 42`. – einpoklum Nov 09 '18 at 22:33