473,229 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,229 software developers and data experts.

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



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
2 1606
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Richard van Wegen | last post by:
Dear All I'm hoping someone can show me the correct way to typedef a template class - i.e. something along the lines of typedef boost::shared_ptr<T> shared_ptr_multithread<T>; (syntax is...
5
by: Alexander Stippler | last post by:
Hello, I wrote an (templatized) operator==() and don't know why the compiler doesn't consider it. What prevents it from being chosen? class Data {}; template <typename D> class Vector {
1
by: Robert J. Hansen | last post by:
I'm exploring the use of type traits (which, I hasten to say, I've hardly ever used before) as a way to help simplify some complex special-case logic in an existing codebase. I'm finding that I'm...
3
by: Erik Wikström | last post by:
I've been trying for a while now to understand how template template parameters work. But I just can't wrap my head around it and was hoping that someone might help me. As best I can figure the...
2
by: Patrick Kowalzick | last post by:
Hello NG, sorry to bother again, but I am a lit surprised that I got no answer on my post (attached below). So I refined the code a little bit :-). If there is a typedefed class X inside a...
4
by: nutty | last post by:
Dear group, I tested this code in a 4.x gcc, MSVC 8.0, 7.1 and comeau in strict and relaxed mode. It compiles in MSVC and relaxed comeau, but fails in gcc 4.x and strict comeau It seems...
4
by: AndrewD | last post by:
Hey C++ folks, I created this today, just for fun. You can make object allocation for any class around 6 times faster, simply by doing the following. class MyClass : public...
5
by: Gianni Mariani | last post by:
I'm hoping someone can tell me why using member address of works and why using the dot operator does not in the code below. The code below uses the template function resolution mechanism to...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.