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
    Posts
    17

    vector as private data

    hey,

    i'm just writing a small class now and i want a few vectors as private data. if i use the line
    Code:
     	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
    Code:
     	std::vector<int> a(32, 0);
    but it won't let me and errors with some
    Code:
     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
    Neo is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    8
    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
    Posts
    8,893
    You can't call constructors directly in the class declaration, you must do it in the constructor chain of the class constructor.
    Code:
    class mine {
      std::vector<int> vec;
    public:
      mine() : vec(32) {}
    };
    All the buzzt!
    CornedBee

    "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-14-2008, 11:16 PM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  4. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM
  5. Accesing private data from method of another object
    By QuickSilver in forum C++ Programming
    Replies: 9
    Last Post: 01-16-2002, 03:15 AM

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