473,320 Members | 1,817 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,320 software developers and data experts.

typedef'd return type of a template class member function: take 2

The following code won't compile for me, it does not
recognize my typedef'd type as a type:

template <class T> class A {
public:
typedef int TD;
private:
TD b ();

};

template <class T> A<T>::TD A<T>::b () {

// Stuff.

}

This would work fine if it was not a template class, it's something I
use regularly to keep my class's types clear and consistent without
polluting their containing namespace. Can anyone tell me how I can
accomplish this with a template?

Thanks!
Dave Corby

P.S.
Sorry about the lack of indentation, I'm at work and can't install a
real NNTP client so I'm posting this through Google.

Jul 22 '05 #1
5 1266
Dave wrote:

The following code won't compile for me, it does not
recognize my typedef'd type as a type:
In general, you should include the exact error message that you got.

template <class T> A<T>::TD A<T>::b () {


Probably need:

template <class T> typename A<T>::TD A<T>::b () {

A<T>::TD is assumed to name a data object unless you tell the compiler
that it's the name of a type.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #2
"Dave" <da*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
The following code won't compile for me, it does not
recognize my typedef'd type as a type:

template <class T> class A {
public:
typedef int TD;
private:
TD b ();

};

template <class T> A<T>::TD A<T>::b () {

// Stuff.

}


You need to tell the compiler that TD is the name of a type. Try this:

template <class T> typename A<T>::TD A<T>::b ()
{
// Stuff.

}

(Note 'typename'...)

Ali

Jul 22 '05 #3
I tried out what you have both suggested, and now I get the error:

error: no `typename A<T>::TD A<T>::b()' member function declared in
class `A<T>'

Now it looks like all I have to do is get the declaration to match the
definition, but I can't imagine how I can change my declaration and
still have it mean the same thing.

Thanks for all your help so far, and sorry about my posting skills, I
am working on them though :)

Jul 22 '05 #4
Oh, so sorry, I found my problem. Your solution fixed it, I just
mistyped a character in the function name and posted again too hastily.
Thank you both again very much!

Jul 22 '05 #5
"Dave" <da*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I tried out what you have both suggested, and now I get the error:

error: no `typename A<T>::TD A<T>::b()' member function declared in
class `A<T>'
This is the complete code that works with g++ both 3.4.2 and 2.95.3:

template <class T> class A
{
public:
typedef int TD;
public:
TD b ();
};

template <class T> typename A<T>::TD A<T>::b ()
{
// Stuff.
}

int main()
{}

Maybe your compiler is too old?
Now it looks like all I have to do is get the declaration to match the
definition,


This is not needed and wouldn't work. You use the 'typename' keyword
whenever a name may change meaning depending on a template parameter. For
example, it is possible that a specialization of the template can introduce
the same name as an object.

Ali

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roger Leigh | last post by:
Although I've got over most of my template-related problems, I'm having trouble when I started to use default template parameters. For template type T, I've typedef'd this as object_type and then...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
11
by: cppaddict | last post by:
Hi, I recently started using typedefs and am curious if they are ever considered evil. In particular, I'm thinking of a situation where a typedef is used in a file but defined in another,...
2
by: marco | last post by:
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...
3
by: david.corby | last post by:
Hi again everyone! I was wondering if there is any way to specify a return type for a template class member function that is typedef'd in the class. I.E... template <class T> class A { ...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
3
by: aaragon | last post by:
Hello everyone, I'm trying to run some simple code but for some reason it doesn't work and I've been staring at it for a long time without a single clue of what's going on. This is what happens,...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.