vector as private data

This is a discussion on vector as private data within the C++ Programming forums, part of the General Programming Boards category; hey, i'm just writing a small class now and i want a few vectors as private data. if i use ...

  1. #1
    Registered User
    Join Date
    Nov 2001

    vector as private data


    i'm just writing a small class now and i want a few vectors as private data. if i use the line
     	std::vector<int> a;
    then there's no problem. But i want to declare that this vector should contain say, 32 elements, and that they are all initialise3d to 0, so i tried
     	std::vector<int> a(32, 0);
    but it won't let me and errors with some
     error C2059: syntax error : 'constant'
    How can i specify the size and initial contents of the vector?

    I don't get any errors if i use code like this in an ordinary program. Is there something special happening here cause it's in the private data section of a class?

    Any ideas would be great.

  2. #2
    Neo is offline
    Registered User
    Join Date
    Nov 2003
    just try specifying in the constructor

    run a loop in the constructor that does it for ya

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    You can't call constructors directly in the class declaration, you must do it in the constructor chain of the class constructor.
    class mine {
      std::vector<int> vec;
      mine() : vec(32) {}
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing private Data members
    By Emeighty in forum C++ Programming
    Replies: 17
    Last Post: 08-15-2008, 12:16 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 07:43 AM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  4. C Programming Question
    By TK in forum A Brief History of
    Replies: 13
    Last Post: 07-04-2002, 08:11 PM
  5. Accesing private data from method of another object
    By QuickSilver in forum C++ Programming
    Replies: 9
    Last Post: 01-16-2002, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21