By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

I have a problem using template and typedef, someone can tell me what is wrong !

P: n/a


the problem:

I use a typedef inside a class template, than I use this type
(dim_v<N1>::Type) to define the argument of a template function f
but when I call this function from main, the compiler (gcc 3.4.1)
tell me: "no matching function found".
If someone, more expert than me, could tell me what is wrong
I would be very happy

the code:

================================================== =============
file: scratch.h
---------------------------------------------------------------
#include <iostream>

typedef int dim_t;
typedef unsigned int rank_t;

template<typename T, rank_t N>
class V
{
private :
T dm1;
rank_t dm2;

public :
V(T p1, rank_t p2) :
dm1(p1), dm2(p2+N)
{
}

rank_t get_2()
{
return dm2;
}
};

template<rank_t N1>
class dim_v
{
public :
typedef V<dim_t, N1> Type;
};

template<rank_t P>
void f(typename dim_v<P>::Type v)
{
std::cout << std::endl << v.get_2() << std::endl;
}

================================================== =============

================================================== =============
file: main.cpp
---------------------------------------------------------------

#include "scratch.h"
int main( int argc, char * argv[] )
{
dim_t type = 5;
rank_t val = 10;
const rank_t r = 3;
dim_v<r>::Type v(type, val); // OK
f(v); // ERROR !

return 0;
}

================================================== =============
# compile command :
g++ -c -o /mnt/TEMP/cpp_projects/SymbolArray/linux/Debug_Build/main.o -g2
-O0 -MD -I/usr/include -I/usr/local/include -I/usr/include/c++/3.4.1
main.cpp

# compile output :
main.cpp: In function `int main(int, char**)':
"main.cpp": main.cpp error: no matching function for call to `f(V<dim_t,
3u>&)' at line 111
GNU C++ Compiler exited with error code: 1

# compiler version :
gcc 3.4.1 (I get the same error also with version 3.3.2)

# machine :
processor: amd athlonxp 1700+
memory: 512MB
os: linux mandrake 10.0


--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"marco" <ma*************@tin.it> wrote in message
news:opsdl07yq10jt8da@localhost...


the problem:

I use a typedef inside a class template, than I use this type
(dim_v<N1>::Type) to define the argument of a template function f
but when I call this function from main, the compiler (gcc 3.4.1)
tell me: "no matching function found".
If someone, more expert than me, could tell me what is wrong
I would be very happy

the code:

================================================== =============
file: scratch.h
---------------------------------------------------------------
#include <iostream>

typedef int dim_t;
typedef unsigned int rank_t;

template<typename T, rank_t N>
class V
{
private :
T dm1;
rank_t dm2;

public :
V(T p1, rank_t p2) :
dm1(p1), dm2(p2+N)
{
}

rank_t get_2()
{
return dm2;
}
};

template<rank_t N1>
class dim_v
{
public :
typedef V<dim_t, N1> Type;
};

template<rank_t P>
void f(typename dim_v<P>::Type v)
{
std::cout << std::endl << v.get_2() << std::endl;
}
I don't think this is allows template argument deduction. The compiler
cannot deduce what "P" should be because it would have to figure things out
in reverse. That is, it would have to figure out what every possible "Type"
is and determine which dim_v<P> holds the V<dim_t, 3> you provided. While
it is obvious which template you mean, when you consider that there could be
a specialization of dim_v<2> that also had a "Type" of V<dim_t,3>, it is not
so obvious. To use argument deduction, try writing it as

template<rank_t P>
void f(V<dim_t,P> v)
{
std::cout << std::endl << v.get_2() << std::endl;
}
================================================== =============
file: main.cpp
---------------------------------------------------------------

#include "scratch.h"
int main( int argc, char * argv[] )
{
dim_t type = 5;
rank_t val = 10;
const rank_t r = 3;
dim_v<r>::Type v(type, val); // OK
f(v); // ERROR !

return 0;
}


If you leave the function template as it is, you should write

f<3>(v);

--
David Hilsee
Jul 22 '05 #2

P: n/a
On Tue, 31 Aug 2004 20:38:18 -0400, David Hilsee
<da*************@yahoo.com> wrote:


I don't think this is allows template argument deduction. The compiler
cannot deduce what "P" should be because it would have to figure things
out in reverse. That is, it would have to figure out what every
possible "Type" is and determine which dim_v<P> holds the V<dim_t, 3>
you provided. While it is obvious which template you mean, when you
consider that there could be a specialization of dim_v<2> that also had
a "Type" of V<dim_t,3>, it is not so obvious.
I have understand what is the problem thank you for the explanation.
I am still a beginner with template so I was not sure what was wrong.
To use argument deduction, try writing it as
template<rank_t P>
void f(V<dim_t,P> v)
{
std::cout << std::endl << v.get_2() << std::endl;
}

Oh yes this work, I already know, but my goal was to get abstraction
on the effectively used type, perhaps it is better to wrap
the whole class V<dim_t, T> in an ad hoc class.

If you leave the function template as it is, you should write

f<3>(v);


Thank you again for help

--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.