Skip to main content
gamedev.net
🔒 Locked

C++ Templates problem

Started by JOL Mar 2, 2004 at 4:25 PM 8 replies 1k views
Original Post
JOL
JOL
I need help, I am obviously missing something (obvious?) regarding templates, haven't been using them to much before. I provide you with an example of the kind of code I am writing, (the actual code is way to large), hopefully it will be a useful library evetually... I can't figure out why this won't compile. I just don't see the problem, (does it have somathing to do with the inheritance or something, maybe templating virtual functions?). When I read Stroustrup (C++ programming language) it seems legit ? I am using Dev-C++ with mingw... (haven't checked out if that is the prob.) Here's the code and the errors.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <limits>
using namespace std;


template &ltclass Type>
class Ttest{
  public:
    std::string name(){return "class Ttest";}
    virtual Type number(Type t)=0;
};

template &ltclass Type>
class Tson: public Ttest&ltType>{
  public:
    Type number(Type t);
};


template <class Type>
Type Tson&ltType>::number(Type t){ 
  return numeric_limits&ltType>::max(); 
}


int main(int argc, char *argv[])
{
  Tson&ltint> d;
  cout << d.name() << endl;
  cout << d.number() << endl;    
  return 0;
} 
ERRORS:
main.cpp:29: no matching function for call to `Tson&ltint>::number()'
main.cpp:21: candidates are: Type Tson&ltType>::number(Type) [with Type = int]
  
[edited by - JOL on March 2, 2004 5:39:28 PM] [edited by - JOL on March 2, 2004 5:40:23 PM]
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
cozman
cozman
It looks like number takes an argument of the template type.

so without templates you basically have:
number(int)
but you're trying to call number()

(ps. for the HTML < make sure you put a semicolon after the &lt, like &lt; )

[edited by - cozman on March 2, 2004 5:40:09 PM]

[edited by - cozman on March 2, 2004 5:40:37 PM]
JOL
JOL
Thank you, I just couldn''t see that error somehow...
The bad part is that my real code still isn''t working...but at least I got my templates straight

Thanks
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
JOL
JOL
The problem is sort of solved, or at least identified. The problem was the need to use the export keyword and since it isn't supported by mingw...
(and 'is to be removed from the standard?')

quote:
from article http://www.comeaucomputing.com/iso/promises.html
Some people naturally expect something like export to work, in fact, they expect it to work without even having to use the export keyword, so they are confused when they get a link error instead
-He is talking about me...

So, how do you go about this ?
A soultion suggested is to implement everything in the .h file or to #include the .cpp code into the.h file. This would work OK since my files are small -so far...
Any other way of forcing the compiler to really compile what I want ?
-how do STL do it ?

[edited by - JOL on March 3, 2004 6:11:26 PM]
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
Fruny
Fruny
quote:
Original post by JOL
So, how do you go about this ?
A soultion suggested is to implement everything in the .h file or to #include the .cpp code into the.h file. This would work OK since my files are small -so far...



Stick everything in the header file. Do not include the cpp file - or if you do, don't call it a '.cpp' file - you could call them 'foo.h' and 'foo.impl' if you want... though there is not much point in splitting the files anyway.

quote:
Any other way of forcing the compiler to really compile what I want ?


No.

quote:
-how do STL do it ?


The STL is integrally implemented as header files (yes, that means you can read the source). On some platforms (e.g. STLPort), instanciations of IOstream classes (like std::basic_istream<char>), as opposed to the templates themselves, are actually compiled into libraries.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on March 3, 2004 6:19:17 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Xai
Xai
no "common" production compiler supports the "export" keyword correctly, or any form of template compilation ... (they do support PRECOMILED HEADERS, which give some of the benifits, sometimes).

So for now, you should always completely implement your templates in the header file ...

I personally just implement my template classes as .hpp and .cpp pairs like any other class ... BUT, at the bottom of the .hpp I #include the .cpp - this way I use the code organization I want, and the client includes the header like they want ... only the header of the template knows it''s doing anything special (WEIRD even) .. and someday, when export works, it will quit #including the .cpp and start using export.
JOL
JOL

Thank you so much for your replies...really helpful.

Thanks

[edited by - JOL on March 3, 2004 6:24:39 PM]
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
JOL
JOL
So, while on the subject ...hrmm...
How do you handle inheritance with pure virtual methods, since it isn't allowed ?
(if I have understood my books right)

example:

template <class T> Parent{ virtual void f()=0;}
template <class T> Child: Public Parent<T>{ void f(){int two=1+1.9;}

void g(){
Child c;
c.f()
}



[edited by - JOL on March 4, 2004 6:28:22 AM]
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
Xai
Xai

template <class T>
class Parent
{
public:
virtual void f() = 0;
}

template <class T>
class Child: public Parent<T>
{
public:
void f()
{ int two=1+1.9; }
void g()
{ Child c; c.f() }
}

template <typename T> // just showing you an alternate to "class" for templates, this is now prefered

class GrandChild: public Child<T>
{
public:
void f()
{ int two=1+1.9; }
virtual void h()
{ Child c; c.f(); }
}


I placed the code in [ source ] (remove spaces) tags, and added the elements I thought we''re eaten by the forum, and the code looks 100% fine to me ... I personally add a "virtual" declaration at every level - but that is neither required, not does it have any real effect on the compiler ... so ..

Here''s the deal ... a class INHEIRITS all public and protected members of it''s base class ... and MUST have some implementation to call for each virtual function, else it cannot be instantiated. But if the function was implemented in the parent class, that implementation is also inheirited if no override is provided ... so your code is fine

... and so is the class GrandChild I added. It has a member f() overriden by itself, a member g() inheirited from Child, and a member h() added by it. Also not that you did not make g() virtual, which you may have wanted to.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.