Thread: Templated Classes with Templated Functions

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    Templated Classes with Templated Functions

    Based on reading here:

    Cprogramming.com - Tutorials - Templated Functions

    I understand that such thing is possible, however I have the following code:

    Code:
    template <int X, int Y>
    class Simulator
    {
    public:
      template<int N, int N1>
      void doom::vector<State, N>& run(doom::vector<Input, N1>& input, double ts);
    protected:
      //some variables here
    }
    
    
    //here's my implementation
    template <int X, int Y>
    template<int N, int N1>
    const doom::vector<State, N>& Simulator<X,Y>::run(doom::vector<Input, N1>& input, double ts);
    {
     //some code here
     return x;
    } 
    
    
    //here's my test
    int main(){
    Simulator<25, 25> s( new Vehicle( WB ) );
    doom::vector<Input, 25> u = InputLibrary::getSimpleInputSet( );
    //this is where the error happened
    doom::vector<State, 25> x = s.run( u, 0.1 );
    }

    my error is the following:
    error: no matching function for call to âSimulator<25, 25>::run(ece373::vector<ece373::vector<double, 2>, 25>&, double)â
    Last edited by -EquinoX-; 11-22-2009 at 05:13 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You need to specify the template parameters to run() explicitly because N is not determinable from the parameters.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I've edited my error above, can you show me what you mean King Mir

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Code:
    s.run<25,25>( u, 0.1 );
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    can someone tell me what's wrong with this:

    Code:
    template <int X, int Y>
    Simulator& 
    Simulator<X,Y>::operator=( const Simulator& other )
    {
    	if( &other != this )
    	{
    		delete v;
    		v = new Vehicle();
    		*v = *other.v;
    		x = other.x;
    		u = other.u;
    	}
    	
    	return *this;
    }
    error is

    expected constructor, destructor, or type conversion before â&â token

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    template <int X, int Y>
    Simulator<X, Y>& 
    Simulator<X,Y>::operator=( const Simulator& other )
    I suppose the template arguments are not strictly necessary for the argument (e.g once it becomes clear it is a member function, Simulator will be implicitly treated as Simulator<X, Y>, but you may add it there as well.
    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).

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    thanks

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and how do I fix undefined reference errors:

    undefined reference to `Simulator<25, 25>::Simulator(Vehicle*)'
    undefined reference to `Simulator<25, 25>::~Simulator()'

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to define those functions.
    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

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by -EquinoX- View Post
    can someone tell me what's wrong with this:

    Code:
    template <int X, int Y>
    Simulator& 
    Simulator<X,Y>::operator=( const Simulator& other )
    {
    	if( &other != this )
    	{
    		delete v;
    		v = new Vehicle();
    		*v = *other.v;
    		x = other.x;
    		u = other.u;
    	}
    	
    	return *this;
    }
    Again here is code that checks for self-assignment, which is a bad thing to need to check for. I shall reiterate again that this code is not exception safe because the new statement might throw an exception, but v is already deleted. Then when the Simulator is destructed it would attempt to delete "v" a second time, leading to a fatal program termination. Read these two articles for a full explanation.
    GotW #11: Object Identity
    GotW #23: Object Lifetimes - Part II

    Not only that, but the actual code of the assignment operator really leaves one thinking that there is no point in v being a pointer rather than just a member. Afterall, this assignment operator already assumes that the pointer is not NULL, is dynamically allocated, and has an assignment operator of its own.
    I would suggest making "v" an object rather than a pointer and deleting this assignment operator entirely as everything in the class is then already capable of assignment through the compiler generated version.
    The only reason not to would be if Vehicle doesn't have a non-throwing swap method but Simulator does.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes and functions
    By cpudaman in forum C++ Programming
    Replies: 20
    Last Post: 12-18-2007, 02:45 AM
  2. Help w/ classes and functions
    By Relikie in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2007, 08:42 AM
  3. Cannot declare STL iterators in custom templated classes!
    By Maelstrom in forum C++ Programming
    Replies: 8
    Last Post: 03-03-2006, 07:58 PM
  4. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 11:19 AM
  5. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM