Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Templates 4 a Nu-B

Reply
Thread Tools

Templates 4 a Nu-B

 
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      12-27-2012
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?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-27-2012
On 12/27/2012 12:34 PM, Rene Ivon Shamberger wrote:
> 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<T>(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?


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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates within templates Tom McCallum C++ 2 08-04-2004 04:44 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



Advertisments