Thread: Vector data in class?

  1. #1
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Vector data in class?

    Q:

    How would I go about making a vector of structs a private data member of a class?

    I tried putting it in as just:

    Code:
    private:
    vector<Trick> myTricks(1);
    But I get errors to the effect of:

    'Invalid use of undefined type'
    'Forward declaration of class'
    'Template argument 1 is invalid'
    'Template argument 2 is invalid'
    'Invalid data member initialization'
    'ISO C++ forbids declaration of myTricks with no type'

    myTricks is defined as:

    Code:
         
       struct Trick {
               
          string name;
          int time;
          int diff;
       };
    If it matters, this is declared in class Pet, which is a subclass of Animal and a superclass of Dog and Cat.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You can't call the vector constructor like that, you'll have to call it from the outer classes constructor (using an initialiser list). Also ensure that Trick is declared fully before it is used, otherwise you'll be limited to vectors containing pointers to Tricks (using a limited forward declaration).

  3. #3
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Outer class' constructor? I do not understand. Same goes for forwards declaration. A vector of pointers to Tricks would be more tedious to manage, but possible I suppose.

    Would this mean I would have to dynamically allocate each Trick, and deallocate them all in Pet::~Pet()? Would this cause a problem if the Pet itself were dynamically allocated?

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Outer class' constructor? I do not understand

    If a vector (an instance of a class) is the member of another class then the 'other' class will be the outer. -

    class Outer
    {
    private:
    vector<Trick> myTricks;
    public:
    Outer():myTricks(1){}
    };


    >Same goes for forwards declaration

    Is the full declaration of Trick available to Outer? Have you either declared Trick before Outer or included Tricks header file in Outers?

    >A vector of pointers to Tricks would be more tedious to manage, but possible I suppose (or you really need reference semantics).

    Yes, but not really necessary unless you have a self refering data structure.

    >Would this mean I would have to dynamically allocate each Trick, and deallocate them all in Pet::~Pet()? Would this cause a problem if the Pet itself were dynamically allocated?

    It doesn't matter how an object is allocated, if it exists it can behave in the same manner.

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    AHH

    Nevermind, I'm just a moron.

    I had a function in Pet called Trick, that was throwing everything off.

    I changed the name and then my previous syntax was correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM