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

Discussion in 'C++' started by marco, Aug 31, 2004.

  1. marco

    marco Guest

    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/
    marco, Aug 31, 2004
    #1
    1. Advertising

  2. marco

    David Hilsee Guest

    "marco" <> wrote in message
    news:eek:psdl07yq10jt8da@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
    David Hilsee, Sep 1, 2004
    #2
    1. Advertising

  3. marco

    marco Guest

    On Tue, 31 Aug 2004 20:38:18 -0400, David Hilsee
    <> 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/
    marco, Sep 2, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlnYWw=?=

    Can someone tell me what is wrong here?

    =?Utf-8?B?QmlnYWw=?=, Aug 11, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    590
    =?Utf-8?B?QmlnYWw=?=
    Aug 12, 2005
  2. Dildo Boy
    Replies:
    6
    Views:
    310
    Flash Gordon
    Mar 6, 2007
  3. E-Dot
    Replies:
    15
    Views:
    516
  4. Cathryn Lindner

    Can someone tell me what I'm doing wrong?

    Cathryn Lindner, Nov 4, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    115
    Cathryn Lindner
    Nov 4, 2003
  5. What-a-Tool
    Replies:
    4
    Views:
    598
    What-a-Tool
    Jul 2, 2004
Loading...

Share This Page