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

member template methods

How come I get a "error C2062: type 'int' unexpected" error with the
second version?

Is it possible to achieve the same functiuonaility as the fn as a
class method?

Many thanks

//this is ok
template <class T>
T* CreateAnInstanceFn()
{
return new T();
}

//but this isn't
class MyClass
{
public:

template <class T>
T* CreateAnInstance()
{
return new T();
}

};
int main()
{
//legal
int* j = CreateAnInstanceFn<int>();

//uh oh!
MyClass mc;
int* i = mc.CreateAnInstance<int>();

return 0;
}
Jan 6 '06 #1
6 6165
Can you try with typename ?

class MyClass {
public:
template <typename T> T * createAnInstance () {
return new T ();
}
};
Jan 6 '06 #2

Stephane Wirtel wrote:
Can you try with typename ?

class MyClass {
public:
template <typename T> T * createAnInstance () {
return new T ();
}
};


typename and class are synonyms when used inside <>s after the keyword
"template". So that should not solve the problem (if there was a
problem at first place)

To the OP - The code in your post compiles well on Comeau online. May
be the problem lies with your compiler.

Jan 6 '06 #3
On 6 Jan 2006 05:46:19 -0800, "Neelesh Bodas"
<ne***********@gmail.com> wrote:

Stephane Wirtel wrote:
Can you try with typename ?

class MyClass {
public:
template <typename T> T * createAnInstance () {
return new T ();
}
};


typename and class are synonyms when used inside <>s after the keyword
"template". So that should not solve the problem (if there was a
problem at first place)

To the OP - The code in your post compiles well on Comeau online. May
be the problem lies with your compiler.

I'm using MSVC6... :o(
Jan 6 '06 #4
Floogle wrote:
On 6 Jan 2006 05:46:19 -0800, "Neelesh Bodas"
<ne***********@gmail.com> wrote:
[..]
To the OP - The code in your post compiles well on Comeau online. May
be the problem lies with your compiler.


I'm using MSVC6... :o(


Upgrade. VC++ v6 is notoriously bad with member templates. Or fall
back onto your work-around (by keeping your function non-member).

V
Jan 6 '06 #5

Stephane Wirtel wrote:
Can you try with typename ?

class MyClass {
public:
template <typename T> T * createAnInstance () {
return new T ();
}
};


typename and class are synonyms when used inside <>s after the keyword
"template". So that should not solve the problem (if there was a
problem at first place)

To the OP - The code in your post compiles well on Comeau online. May
be the problem lies with your compiler.

Jan 7 '06 #6
Floogle wrote:
On 6 Jan 2006 05:46:19 -0800, "Neelesh Bodas"
<ne***********@gmail.com> wrote:

Stephane Wirtel wrote:
Can you try with typename ?

class MyClass {
public:
template <typename T> T * createAnInstance () {
return new T ();
}
};


typename and class are synonyms when used inside <>s after the keyword
"template". So that should not solve the problem (if there was a
problem at first place)

To the OP - The code in your post compiles well on Comeau online. May
be the problem lies with your compiler.

I'm using MSVC6... :o(


VC6 is known to have many template problems. You can consult the MS
newsgroups for more details, but one solution is to convert the
template type specification to automatic type deduction through
overloading by using this trick from _Modern C++ Design_:

template<typename T>
struct Type2Type { typedef T Type; };

class MyClass
{
public:
template <class T>
T* CreateAnInstance( Type2Type<T> )
{
return new T();
}
};

int main()
{
MyClass mc;
int* i = mc.CreateAnInstance( Type2Type<int>() );
return 0;
}

You might also make MyClass::CreateAnInstance() either static or const,
depending on what your real code does. Also, you might consider using
returning a smart pointer (e.g., std::auto_ptr) rather than a raw
pointer to help prevent leaks:

#include <memory>
using namespace std;

class MyClass
{
public:
template <class T>
static auto_ptr<T> CreateAnInstance( Type2Type<T> )
{
return auto_ptr<T>( new T() );
}
};

Cheers! --M

Jan 7 '06 #7

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

Similar topics

6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
0
by: Gianni Mariani | last post by:
I remember seeing a neat template specialization trick posted here a few months ago that allowed the detection of a type containing a member. After a day searching through google archives I come up...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
9
by: Ian | last post by:
Can it be done? If so, what's the syntax. For example a full specialisation, template <typename T> struct X { template <typename C> void foo( C a ) {} };
1
by: Andrew Wilkins | last post by:
Hi, C++ forbids templated virtual member functions. For example, the following will not (should not - it doesn't in GCC 3.4.3) compile: <snip> template <class T> class Example { public:
6
by: Felix I. Wyss | last post by:
It appears that VC++2003 has a code generator bug related to template parameters that are a pointer-to-member type: If the actual template argument is a virtual method, VC generates code that...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
5
by: pkoniusz | last post by:
Hello everyone. The problem may be obvious, though I'm a bit puzzled by the error LNK2028 when attempting to utilize my static library. The all methods of the class defined within that library...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.