Vectors of pointers and function templates

This is a discussion on Vectors of pointers and function templates within the C++ Programming forums, part of the General Programming Boards category; Hi all, Im trying to write a function template that takes a reference to a vector of pointers to objects. ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    2

    Vectors of pointers and function templates

    Hi all,

    Im trying to write a function template that takes a reference to a vector of pointers to objects. ...if that makes sense.

    so far i have in my code (sort of)...

    template < class T >
    void MyFunc(std::vector < T * > &aVector);

    int main()
    {
    .
    .
    .
    std::vector<MyClass *> vMyObject;
    MyFunc(vMyObject);
    .
    .

    }

    template < class T >
    void MyFunc(std::vector < T * > &aVector)
    {
    T *pStuff = new T(3,3);
    aVector.push_back(pStuff);
    }



    dont ask y im doing it, i just need to do it. i Get an Internal Compiler Error using VC++.
    Any ideas anyone?

    Cheers!

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    1. Read the first few sticky posts on this board.
    2. Do a search on this board before posting
    3. this might help you: is push_back() a overloaded function ?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    2

    thats great...but

    i dont think you appreciate what im getting at.
    -ok sorry not using code tags, but i did search.
    anyway.

    the problem is with the function template not the vector or the push_back function. ill explain a litte more...

    i have three kinds of objects.
    i have three vectors which hold pointers to objects.
    i want to write a function that adds new objects to one of the vector
    i dont want to write three functions, so i want to use a function template.

    after a bit more playing around ive got this...

    Code:
    template < class T >
    void MyFunc(std::vector < T > &aVector);
    
    int main()
    {
    .
    .
    .
            std::vector<MyClass *> vMyObject;
            MyFunc(vMyObject);
    .
    .
    
    }
    
    template < class T >
    void MyFunc(std::vector < T > &aVector)
    {
          T Obj;  //pointer to object since T = MyClass*
          aVector.push_back(Obj);
    }

    Now this works, but the trouble is the new object has no values because im not using the new keyword.
    im creating a pointer to an object, and i know i need to get the "new" keyword in there somewhere...

    Code:
          T Obj;
          Obj = new T;   //this would work if T wasnt a pointer
          aVector.push_back(Obj);
    so there we go...
    anymore ideas?

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You were on the right track with your first attempt. The one thing you need to be aware of is making sure that all classes you use with this have the same constructor.

    Here is a short example:
    Code:
    #include <iostream>
    #include <vector>
    
    struct A
    {
      A() { std::cout << "A created" << std::endl; }
    };
    
    struct B
    {
      B() { std::cout << "B created" << std::endl; }
    };
    
    template<class T>
    void func(std::vector<T*>& vec)
    {
      T* obj = new T; // Uses default constructor.
      vec.push_back(obj);
    }
    
    int main()
    {
      std::vector<A*> vec1;
      std::vector<B*> vec2;
    
      func(vec1);
      func(vec2);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

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