Problem with templates and return type

N

none

I have the following in a header file:


template<typename T>
class Sub : public MyBase<T>
{
public:
// Types
typedef MyBase<T> Superclass;
typedef typename Superclass::ImageType ImageType;

// Modifiers
ImageType & GetImageData();

};



In the .cpp file I then do:

template<typename T>
ImageType &
Sub<T>::GetImageData()
{
// return null;
}


But I get the error:

error: expected constructor, destructor, or type conversion before '&' token

Why this error? ImageType has been typedef'ed.
 
V

Vladimir Jovic

none said:
I have the following in a header file:


template<typename T>
class Sub : public MyBase<T>
{
public:
// Types
typedef MyBase<T> Superclass;
typedef typename Superclass::ImageType ImageType;

// Modifiers
ImageType & GetImageData();

};



In the .cpp file I then do:

template<typename T>
ImageType &
Sub<T>::GetImageData()
{
// return null;
}


But I get the error:

error: expected constructor, destructor, or type conversion before '&'
token

Why this error? ImageType has been typedef'ed.

Template dependent lookup name.
This is correct:

template<typename T>
typename Sub<T>::ImageType &
Sub<T>::GetImageData()
{
// return something;
}
 
N

none

none said:
I have the following in a header file:


template<typename T>
class Sub : public MyBase<T>
{
public:
// Types
typedef MyBase<T> Superclass;
typedef typename Superclass::ImageType ImageType;

// Modifiers
ImageType & GetImageData();

};



In the .cpp file I then do:

template<typename T>
ImageType &
Sub<T>::GetImageData()
{
// return null;
}


But I get the error:

error: expected constructor, destructor, or type conversion before '&'
token

Why this error? ImageType has been typedef'ed.


This solved the error:

template<typename T>
typename Sub<T>::ImageType &
Sub<T>::GetImageData()
{
// return null;
}

Who came up with this monstrous way of defining C++ template functions?
 
J

James Kanze

In the class. In the above, ImageType is used before you're in
the context of the class.
This solved the error:
template<typename T>
typename Sub<T>::ImageType &
Sub<T>::GetImageData()
{
// return null;
}
Who came up with this monstrous way of defining C++ template
functions?

It has nothing to do with templates. You'll have the same
problem with a non-template class:

class Sub : public MyBase
{
public:
typedef MyBase::ImageType ImageType;
ImageType& GetImageData();
};

ImageType&
Sub::GetImageData()
{
return NULL;
}

The compiler doesn't know to start looking in Sub until it
reaches the point where it knows that it is compiling a member
function of Sub.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top