Thread: vector of type template cass

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    vector of type template cass

    Hi all,

    Code:
    template <typename T>
    void
    FooFunc (vector< FooClass<T> > v)
    {
       vector < FooClass<T> >::iterator it;
      
    }
    I'm trying to do that, but I get an error, is this possible? If yes in which conditions? Do I have to give it a concrete class type like FooClass<float> ??

    The error says "expected ; before it"

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but here iterator is a name that depends on a template name: you know that it is a type name, but it could be say, a member name. You should use typename to disambiguate:
    Code:
    typename vector < FooClass<T> >::iterator it;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    Oh, I really wasn't aware of this. Thank you for the explanation.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    I have another doubt, how can I modify an element in vector and what is the best way to do it?
    See the example below, to see what I'm trying to do.

    Code:
    template <typename T>
    void
    FooFunc (vector< FooClass<T> > v)
    {
       vector < FooClass<T> >::iterator it;
       FooClass<T> tmp;
    
       for (it = v.begin(); it != v.end(); it++)
       {
          tmp = changeSomething(*it);
       }
      
       // How to replace the element??
       *it = tmp; // fails, because this will change the iterator.
      // Use copy or replace by having and temporary vector??
      // alternatives??
    }

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose you want:

    Code:
       for (it = v.begin(); it != v.end(); it++)
       {
          *it = changeSomething(*it);
       }
    Or perhaps:

    Code:
       std::transform(v.begin(), v.end(), v.begin(), changeSomething);
    It's likely that you don't want to pass a copy of the vector to FooFunc either.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    Yes, you're correct, it should be passed by reference to avoid unnecessary copying.

    I will look into std::transform, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  4. Template specialization
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2003, 02:02 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM