typedef, inheritence, templates

First time poster.

I have just been introduced to boost and shared_ptr and having a few difficulties getting it to work with template functions and inheritence

I have the following classes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Object
{
public:
   typedef boost::shared_ptr<Object> Ptr;
};

class MyObject : public Object
{
public:
   typedef boost::shared_ptr<MyObject> Ptr;
};

class Service
{
    template<typename T>
    T::Ptr doSomething();
}

The problem arises when I do the following:
1
2
Service svc;
MyObject::Ptr result = svc.doSomething<MyObject>();

Because the typedef is defined in both Object and MyObject with the same name the compiler rightfully says the typedef is ambiguous, but in order to use the templated method they have to have the same name

My current solution is to remove the typedef from the base class Object and put it outside the scope of the class definition. I am not very happy about this solution since I can no longer do the following:
 
Object::Ptr result = svc.doSomething<Object>();

I assume this is a common problem and there is a simple solution?

regards
You need to add typename on line 16 ( typename T::Ptr doSomething(); )
The derived typedef should shadow the base typedef, adding 'typename' as stated above compiles fine with g++
Sorry - missed out the typename declaration.

I am using visual studio 2008. That actual error is:

error C2385: ambiguous access of 'Ptr'

even if I was to declare a variable I would get the same error:

 
MyObject::Ptr variable;


I would get the same error
typedef isn't good. Why not making it the real thing:

1
2
3
4
5
class Service
{
    template<typename T>
    boost::shared_ptr<T> doSomething() { // Note: needs a body for the template }
};
Topic archived. No new replies allowed.