Templates 4 a Nu-B

  • Thread starter Rene Ivon Shamberger
  • Start date
R

Rene Ivon Shamberger

In a class I declared a template function


---myClass.hpp---
#ifndef MYCLASS_HPP
#define MYCLASS_HPP

namespace ABC{
class myClass{
....
public:
myClass();
~myClass();
void someMethod();
template< typename T> std::string& myTemplate(const T& data);
}; // class
} // namespace
#include "myClass.tcc"
#endif

---myClass.cpp---
ABC::myClass::myClass(){/*nothing*/}
ABC::myClass::~myClass(){/*nothing*/}
ABC::myClass::someMethod(){/*nothing*/}


In a tcc file I impletemtn the template function

---myClass.tcc--
template< typename T>
std::string& ABC::myClass::myTemplate(const T& data){
...
return someString;
}

VC++ gives me an error that reads:
error C2039: 'myTemplate' : is not a member of 'ABC'

I tried this:
namespace ABC{
template< typename T>
std::string& ABC::myClass::myTemplate(const T& data){
...
return someString;
}
}

but to no avail.
What am I am doing wrong?
 
V

Victor Bazarov

In a class I declared a template function


---myClass.hpp---
#ifndef MYCLASS_HPP
#define MYCLASS_HPP

namespace ABC{
class myClass{
....
public:
myClass();
~myClass();
void someMethod();
template< typename T> std::string& myTemplate(const T& data);
}; // class
} // namespace
#include "myClass.tcc"
#endif

---myClass.cpp---
ABC::myClass::myClass(){/*nothing*/}
ABC::myClass::~myClass(){/*nothing*/}
ABC::myClass::someMethod(){/*nothing*/}

void ABC::myClass::someMethod(){}
In a tcc file I impletemtn the template function

---myClass.tcc--
template< typename T>
std::string& ABC::myClass::myTemplate(const T& data){

Should probably be

template<typename T>
std::string& ABC::myClass::myTemplate said:
...
return someString;
}

VC++ gives me an error that reads:
error C2039: 'myTemplate' : is not a member of 'ABC'

I tried this:
namespace ABC{
template< typename T>
std::string& ABC::myClass::myTemplate(const T& data){
...
return someString;
}
}

but to no avail.
What am I am doing wrong?

I am guessing, but most likely you're doing something that you're not
telling us. I just took your code, put it all in one file and compiled
with VC++ 2012 *successfully*. Here is the code:

#include <string>

namespace ABC{
class myClass{
//
public:
myClass();
~myClass();
void someMethod();
template< typename T> std::string& myTemplate(const T& data);
}; // class
} // namespace

template< typename T>
std::string& ABC::myClass::myTemplate(const T& data){
return someString;
}

ABC::myClass::myClass(){/*nothing*/}
ABC::myClass::~myClass(){/*nothing*/}
void ABC::myClass::someMethod(){/*nothing*/}

int main()
{
ABC::myClass mc;
}

Now, 'someString' is undefined inside the 'myTemplate' member function,
but that fact goes unnoticed because there is no *use* of 'myTemplate'
member anywhere in the program, so the compiler only performs some
syntactic analysis of the 'myTemplate's body.

Perhaps if you posted a more complete program, we could help you more.

V
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top