Template member function

P

pveglia

Hi everyone,
i've got a problem with a template function member of a non-template
class. My code is the following:

class PropMap
{
private:
std::map<std::string, void*> properties;
public:
template<typename T>
T getProperty(std::string key)
{
return reinterpret_cast<T>(properties[key]);
}


T setProperty(std::string key, T value)
{
return reinterpret_cast<T>(properties[key] = value);
}
};

when i try to call the "getProperty" method:

int* i = getProperty<int*>("pos");

I get the error: "expected primary function". I think gcc doesn't know
that getProperty() is a template by I don't know why. Is there any
workaround to fix this?
Thank you very much.
Paolo
 
R

Robert Bauck Hamar

Hi everyone,
i've got a problem with a template function member of a non-template
class. My code is the following:

class PropMap
{
private:
std::map<std::string, void*> properties;

std::map not declared
std::string not declared

You forgot to #include said:
public:
template<typename T>
T getProperty(std::string key)
{
return reinterpret_cast<T>(properties[key]);
}

template said:
T setProperty(std::string key, T value)
{
return reinterpret_cast<T>(properties[key] = value);
}
};

when i try to call the "getProperty" method:

int* i = getProperty<int*>("pos");

PropMap pm;
int *i = pm.getProperty<int*>("pos");

compiles on my g++. You should post a minimal, but complete program that
shows the error.
I get the error: "expected primary function". I think gcc doesn't know
that getProperty() is a template by I don't know why.

It isn't. PropMap::getProperty is.
 
P

Paolo Veglia

Robert said:
Hi everyone,
i've got a problem with a template function member of a non-template
class. My code is the following:

class PropMap
{
private:
std::map<std::string, void*> properties;

std::map not declared
std::string not declared

You forgot to #include said:
public:
template<typename T>
T getProperty(std::string key)
{
return reinterpret_cast<T>(properties[key]);
}

template said:
T setProperty(std::string key, T value)
{
return reinterpret_cast<T>(properties[key] = value);
}
};

when i try to call the "getProperty" method:

int* i = getProperty<int*>("pos");

PropMap pm;
int *i = pm.getProperty<int*>("pos");

compiles on my g++. You should post a minimal, but complete program that
shows the error.
I get the error: "expected primary function". I think gcc doesn't know
that getProperty() is a template by I don't know why.

It isn't. PropMap::getProperty is.
Thank you for your reply!

I wrote a little esample using that class and it works, but I still
can't compile my program. Unfortunately I'm not able to reproduce the
error with an example.
 
J

Jim Langston

Paolo Veglia said:
Robert said:
Hi everyone,
i've got a problem with a template function member of a non-template
class. My code is the following:

class PropMap
{
private:
std::map<std::string, void*> properties;

std::map not declared
std::string not declared

You forgot to #include said:
public:
template<typename T>
T getProperty(std::string key)
{
return reinterpret_cast<T>(properties[key]);
}

template said:
T setProperty(std::string key, T value)
{
return reinterpret_cast<T>(properties[key] = value);
}
};

when i try to call the "getProperty" method:

int* i = getProperty<int*>("pos");

PropMap pm;
int *i = pm.getProperty<int*>("pos");

compiles on my g++. You should post a minimal, but complete program that
shows the error.
I get the error: "expected primary function". I think gcc doesn't know
that getProperty() is a template by I don't know why.

It isn't. PropMap::getProperty is.
Thank you for your reply!

I wrote a little esample using that class and it works, but I still
can't compile my program. Unfortunately I'm not able to reproduce the
error with an example.

I find it hard to believe you can't reproduce the error with an example.
Perhaps the example needs to be extremely large (like all your source) but
it should be possible. Without seeing what you're trying, we can't help.
 

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,781
Messages
2,569,615
Members
45,296
Latest member
HeikeHolli

Latest Threads

Top