Thread: Templates question

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    60

    Templates question

    Hi,
    Suppose i have defined the following templated class:
    Code:
    template <class mytype> struct myclass{
                          mytype id;                    
                          string name;};
    When i define a function of another class that returns a myclass item should i define the mytype type or not?
    In other words which of the following is correct?
    Code:
    myclass AnotherClass::function(){}
    or
    Code:
    myclass <int>  AnotherClass::function(){}
    Thanks..

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    myclass<int> AnotherClass::function() {}
    Or you could use a template parameter.
    Code:
    template <typename T>
    myclass<T> AnotherClass::templateFunction() {}
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is not a question of either one or the other. Your design should make only one of those options appropriate.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    I understand..
    One more thing:
    Is it necessary for the class in which a templated function is declared to be also declared as templeted?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    As is my understanding, your design will make either a templated class okay, or templated free functions okay. Avoid using templates on members of a non-templated class.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Another question:
    Suppose i need to use again the below structure:
    Code:
    template <class mytype> struct myclass{
                          mytype id;                    
                          string name;};
    And i want to define a vector of the above class. I tried this but i get some compilation errors:
    Code:
    vector <myclass<int>> a_vector(24);
    May someone show me the right way?
    Thanks...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Currently, C++ greedily matches such that the ">>" is considered a single token (i.e. the right shift operator, overloaded or otherwise).

    As a workaround, insert a space:
    Code:
    vector <myclass<int> > a_vector(24);
    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

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    I write the following:
    Code:
    #ifndef SYSTEM_H
    #define SYSTEM_H
    #include "Data.h"        //definition of measurement included
    
    #include <vector>
    using std::vector;
    
    class System{           
                  vector <measurement <int> > a(24); 
                  };
                  
    #endif
    and compiler gives error :
    expected ';' before '(' token.
    May someone tell me why? Shouldn't it be ok?

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Ok i suppose i shouldn't define the vector's length in a header file..

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can't initialize a class variable at its declaration; however, you can do so in the constructor.
    Code:
    class bad {
    public:
        int x = 2;
    };
    
    class good {
    public:
        int x;
        good() : x(2) {}
    };
    In your case, if you wanted a size of 24, you could use
    Code:
    class System{           
                  vector <measurement <int> > a(24); 
    public:
                  System() : a(24);
                  };
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Question about templates
    By grscot in forum C++ Programming
    Replies: 0
    Last Post: 04-29-2003, 10:00 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Stack Question With Templates
    By Anonymous Freak in forum C++ Programming
    Replies: 6
    Last Post: 02-09-2003, 12:18 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM