1

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.

Chad Harrison
  • 2,836
  • 4
  • 33
  • 50
  • 6
    You should never "come from another language". Get a book and learn from the ground up, applying knowledge from other languages will just mess you up. http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – GManNickG Jul 11 '10 at 17:59
  • You are correct sir, and thats actually what I'm doing. Currently using 'C++, The Complete Reference 4th Edition' by Herbert Schildt as my reference. Sometimes small details, such as the problem that I was facing, do not get addressed from these ground up books. I'm sure I would have eventually found the answer with more reading. But semantically, the object-oriented and procedural schools of thought can be carried from language to language, no? – Chad Harrison Jul 11 '10 at 18:32
  • 3
    No, please, not Schildt. Generally thought to be the worst author of books on C++ (and C) ever - Accelerated C++ from the list that GMan supplied is the way to go. –  Jul 11 '10 at 18:37
  • Indeed, sorry for your position but Schildt books are indeed a waste. :/ – GManNickG Jul 11 '10 at 18:41
  • Nice, you live and learn I suppose. I guess that's what I get for browsing at Barnes and Noble and buying on a whim. Will take heed to GMan. Thanks again.. – Chad Harrison Jul 11 '10 at 18:51

2 Answers2

6

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;
  • 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! – Chad Harrison Jul 11 '10 at 18:07
2

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
  • 93,612
  • 16
  • 138
  • 200
  • Thanks for that class example as well! – Chad Harrison Jul 11 '10 at 18:08
  • `int64_t` and `stdint.h` are no more standard that `long long`. – Mike Seymour Jul 11 '10 at 19:26
  • @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 , which addresses that issue). – Michael Aaron Safyan Jul 11 '10 at 19:27
  • 2
    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 Jul 11 '10 at 19:35