1

Something baffles me about C++, I can write something like:

struct Test {
    double V[3];
}

without a problem, but when I try for e.g.:

struct Test {
    vector<double> V(3);
}

the compiler gives me an error. What is the difference between the above, I mean why can't the second one be compiled?

and, this one is even more baffling:

struct Test {
    std::vector<double> V[3];
}

The last example that at least in my mind should throw a compiler error is compiled without a problem by both GCC and Clang.

2
  • 1
    It seems like you want a fixed length array stl type. If this is correct look into std::array Commented Dec 15, 2014 at 17:26
  • I don't want a fixed length array, I was just doing some exploratory coding and thought that this is an inconsistency in the language. Commented Dec 15, 2014 at 17:39

4 Answers 4

4

Do not confuse initialization and declaration.

double V[3];

This declares an array of three doubles. No problem. No initialization.

vector<double> V(3);

This declares a vector and tries to initialize it by calling its constructor with 3 as argument. Initialization like this is not allowed here. With C++11 you can use initialization syntax with curly braces, this is allowed:

vector<double> V{3};

However, since vector has a constructor that takes an initializer_list<double>, this will be interpreted as calling that constructor, not the one that takes a size_t value (the one you want). It will initialize the vector with a single element (3.0). You could use V{0,0,0} for what you want, though this might get ugly once you need more than 3 values.

Your final example

std::vector<double> V[3];

simply declares an array of three vectors. No initialization. No problem.

Sign up to request clarification or add additional context in comments.

6 Comments

why is V(3) not allowed?
V(3) is not allowed because it's currently in a place that wants a type and a variable name and nothing else, such as std::vector<double> V;. V(3) would be allowed in the constructor (or member initialzier list), where that variable needs to be initialized.
To clear any confusion, vector<double> V{3}; does not do what the OP wants. It will create a vector with one element which value is the double 3.0.
Thanks SirDarius, I think gexicide meant: vector<double> V{0, 0, 0}; which is the equivalent of double V[3]; using std::vector.
Or vector<double> V = vector<double>(3);, which will be much less ugly comparatively if you need 100 and not 3 elements. The grammar there requires a brace-or-equal-initializer because the parentheses version may cause ambiguities.
|
2

Those are different things.

The first declares an array of three elements. The [3] is part of the type.

The second declares a vector, and then uses something that looks like initialization syntax, but isn't allowed in this particular context. The (3) is not part of the type. (Note that in C++11 or 14 you get in-class initializers that allow something similar.)

The third declares an array of three vectors. Compare the syntax to the first one: all that changes is that double is replaced by std::vector<double>.

Comments

1

It seems like you want a fixed length array stl type. std::vector isn't this. You're looking for std::array

You probably want std::array<double, 3>.

Please disregard if this isn't your intention.

1 Comment

Thanks, but I was simply trying to use a std::vector and not std::array.
1

What you want to do here is:

struct Test {
    vector<double> v;

    Test(): v(3) {}
};

which adds a constructor to your structure with an initialization list that explicitely invokes the vector's constructor with 3 as a parameter.

It is otherwise not possible to call a constructor in the context of declaring structure or class members (excepting initializer lists constructors with the specific C++11 syntax using curly braces).

More generally, fixed-size arrays should use the std::array class, as mentioned before.

1 Comment

Thanks, sorry that I can't upvote you yet (not enough points here).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.