views:

114

answers:

2

Ok, I'm coming from vb.net to c++. Im trying to use vectors in a structure, but the compiler yells at at me for it. What is wrong with the current statement?

#include <vector>

struct FactorSet
{
 vector<long long> UpperFactor(0);
 vector<long long> LowerFactor(0);
};

Output error (Visual Studio 2008):

Error 1 error C2059: syntax error : 'constant'

I venture to guess it is my lack of understanding of what a vector really is. In my mind it is an object, although I think its whats called a template. Other objects like strings seem to have no problem. I also assume this is extended to class definitions as well since structures and classes are so similar.

+6  A: 

You want:

#include <vector>

struct FactorSet
{
 std::vector<long long> UpperFactor;
 std::vector<long long> LowerFactor;
};

though you might also have problems with long long, as this is not currently part of C++.

If you actually want to give a size to the vectors, you need to do that via the structs constructor:

struct FactorSet
{
 std::vector<long long> UpperFactor;
 std::vector<long long> LowerFactor;

 FactorSet() : UpperFactor(42), LowerFactor(42) {}
};

So now when you say:

FactorSet f;

the vectors in f will both have size 42.

As to what a vector actually is, it's a class very like a string, except in the case of the vector you have to say what type of thing it contains. So

vector <char> s;

is very similar (but not identical) to:

string s;
anon
Wow, that was quick. Since I was trying to initialize with zero length in the declaration, I suppose it was being treated as a constant, and structures and classed don't like that in c++. Thanks Neil!
hydroparadise
+2  A: 

Yes, you can use the vector in a struct or class. The problem is that in C++, you cannot initialize members inline; you need to do initialization in the constructor. For example:'

class FactorSet {
   public:
       FactorSet() : UpperFactor(0), LowerFactor(0) {}
       // ...
   private:
       std::vector<int64_t> UpperFactor;
       std::vector<int64_t> LowerFactor; 
};

Just some comments... vector will, by default, be constructed with zero elements, so it is not necesary to explicitly construct it that way (in fact, doing so might be slightly slower than just using the default constructor). Also, the type long long is presently non-standard. Consider using int64_t defined in stdint.h (and, more portably, in the header boost/cstdint.hpp).

Michael Aaron Safyan
Thanks for that class example as well!
hydroparadise
`int64_t` and `stdint.h` are no more standard that `long long`.
Mike Seymour
@Mike, yes they are. They are specified in IEEE Std. 1003.1 POSIX, the OpenGroup Base Specification / Single UNIX Specification, and also the ISO C99 standard. Windows is simply non-compliant with all three standards (which is why I suggested using <boost/cstdint.hpp>, which addresses that issue).
Michael Aaron Safyan
@Michael: OK, maybe they appear in more standards that `long long`, but they are not standard C++. The question was about C++, not POSIX, Unix or C99.
Mike Seymour