Hello,

I was hinted for my cause in using a mastertemplate. I did and I swear my little programm was up and running late night just the way I wanted it. This morning, however, I did some other corner stuff and now it does not compile anymore.. just like that... Have a look please...

#include <iostream>

class A
{
public:
    A():x(0),y(0.0f){}
    void Do(void);

    template <class T1, class T2>
    void Add (int a,
              int b,
              T1 *var1,
              T2 *var2);

    int x;
    double y;
};

template <class T1, class T2>
void A::Add (int a,
             int b,
             T1 *passedVar1=T1(),
             T2 *passedVar2=T2())
{
}

void A::Do(void)
    {
    Add(1,2, &x, &y); //works!
    Add(1,2, &x); //error!
    }

int main ()
    {
    A a;
    a.Do();
    }

Compiler Error@Code::Blocs 8.02

D:\C++ Projects\marc\console\main.cpp||In member function `void A ::Do()': |
D:\C++ Projects\marc\console\main.cpp|30|error: no matching function for call to `A::Add(int, int, int* )'|
||=== Build finished: 1 errors, 0 warnings ===|

I appreciate any help on that!

poliet

Function can't define type of T2.
One way to solve it - it's using class template that indicates all types patently.
Example

template <class A, class B, ..., class Z>
class MyClass
{
//body
};

//initializing
MyClass < int, int,...,float > a;

I hope it will help you.

The problem seem to be that when you get to Add(1,2,&x); . There is no way that the compiler can figure out what T2 should be.

So you can get round that by putting

Add<int,double>(1,2,&x);

or obviously, you can do it if you have something like

template <class T1>
void A::Add (int a,int b,T1 *passedVar1=T1(),
                 T1 *passedVar2=T1())
{}

// Then this compiles:
Add(1,2,&x);

along with appropiate declaration.

HOWEVER: Be VERY careful with this code since as you have written it you are going to get a segmentation fault if you try to access passedVar2.

Thanks so far, that are some really good posts, but yet I cannot make things work easier for me..

I want to pass a certain number of arguments... My actual code is working with a bitmap OpenGL font and I can do something like this:

Add(gEventManager::ENQ_SCREENDIMENSIONS, "Screen Dimensions X: $$  Y: $$", &Camera->rScreenX(), &Camera->rScreenY() );

Add(gEventManager::ENQ_Help, "Help Message: $$  ", &HELP->rLastHelpAsked());

while $$ in the String are being replaced in with the appropriate passed Variables in the right order

then later in the code I call my message class

gMessage->DisplayOnScreen(gEventManager::ENQ_SCREENDIMENSIONS)

gMessage->DisplayOnScrollboard(gEventManager::ENQ_HELP)

This all works fine, except I needed a good Add(..) and not bother of the type of passed variables too much. Sometimes there could be up to 5 passed variables and with different types int, double or string.

Well.. dipping deeper into this topic, I figured, that functions templates cannot have default values.. Thus the only "good" work around I came up it so far, is to overload the function... This compiles fine.

#include <iostream>

class A
{
public:
    A():x(0),y(0.0f){}
    void Do(void);

    template <class T1, class T2>
    void Add (int a,
              int b,
              T1 *var1,
              T2 *var2);

    template <class T1>
    void Add (int a,
              int b,
              T1 *var1);

    int x;
    double y;
};

template <class T1, class T2>
void A::Add (int a,
             int b,
             T1 *var1,
             T2 *var2)
{
}

template <class T1>
void A::Add (int a,
             int b,
             T1 *var1)
{
}

void A::Do(void)
    {
    Add(1,2, &x, &y); //works!
    Add(1,2,&x); //works!
    }

int main ()
    {
    A a;
    a.Do();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.