473,756 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::Typ e) 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<typena me 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 1648
"marco" <ma************ *@tin.it> wrote in message
news:opsdl07yq1 0jt8da@localhos t...


the problem:

I use a typedef inside a class template, than I use this type
(dim_v<N1>::Typ e) 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<typena me 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
17170
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 obviously wrong but you get the idea)
5
1365
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
1376
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 running into some of the sharp corners of the C++ template facility. I'm hoping someone can answer a couple of questions about the following code snippet: ===== template <
3
1747
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 code should look something like this: template<template<typename S> typename T> struct element_traits { typedef S type; };
2
2207
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 class Y which has the same typedef name and used in the form "Instance_Y::type::type" MSVC7.1 fails to compile. See example below.
4
1782
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 it is not possible to derive from a template class and pass it
4
2577
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 TXpQAlloc<MyClass,N> N is a chunking factor (defaults to 10).
5
2592
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 determine wether a class contains a member. My understanding is that if a template function has an error during resolution of the function types, it is quietly eliminated from the resolution process. This can be used to detect things that would...
6
391
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
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.