Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Visual C++ 6.0 template problem (adapters f/ STL) Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
gerbopel

February 02, 2005, 02:44 PM

Hello everyone,

I am having a very weird problem with vc++. If I have the following code (it's messy so it takes up little room):

template
class funcobj : public binary_function
{
public:
bool operator() ( T question, T2 data ) const;
};

template
bool funcobj::operator () ( T question, T2 data ) const

{
return question.m_data == data;
}

Currently, in my project, I have a main.cpp file and then a funcobj.h/funcobj.cpp module pair. If I compile it with the following line in main.cpp:

Iter = find_if( List.begin(), List.end(), bind2nd(funcobj(), 2 );

Then the linker complains about the () operator being unresolved. However, if I ditch the funcobj.h/cpp module pair and put all the code inside main.cpp it works fine.

So, VC++ doesn't like how I'm organizing the classes that are using templates. There must be a way that I can tell VC++ to not worry about it when I organize them the way I wanted to previously.

Does anyone know how?

Thanks,
Gerb

 
gerbopel

February 02, 2005, 03:37 PM

Right, I'll answer my own question.

Templates don't allow you to separate the interface from the implementation. All of the methods must be defined in the template.

It's been a while since I used them and I lied when I said previous projects worked with such a module layout. The module layout I mentioned is fine as long as the templated classes are being used by methods contained in the funcobj.cpp file... not main.cpp

 
Bramz

February 03, 2005, 03:23 AM

that's not completely true. With templates, you can seperate interface from implementation in the following two ways:

1. both are in the same header file (or the header file includes the source file). Some will say this isn't a true seperation. While it's true you show the implementation to the client, you still make a clear seperation of what is interface and what is implemenation

2. If you know all possible types the template can be used with, then you can keep the implementation in a seperate source file, and in that file do an explicit instantiation of the template for all those types. It's obvious this can have severe limitations on the use of the template :)

Bramz

 
Chris

February 03, 2005, 04:03 AM

I like to separate template declaration and implementation by having the declaration in a "hpp" file which #includes an "ipp" file for the implementation.

Of course that's not a real separation, but I find it's cleaner that having to skip over the implementation when reading the header.

It's more of a means of organization.

 
This thread contains 4 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.