Dependency loop with templates

Wai-chung Poon tsdmilan@yahoo.com.hk
Wed Nov 15 14:45:00 GMT 2006


I encountered compilation problems with the following
code:
 
<< A.h >>
#include <vector>
 
namespace Ser
{
    template <class T>
    size_t StreamSize(const std::vector<T> &v)
    {
        size_t Result = 0;
        for (int i = 0; i < v.size(); ++i)
               Result += StreamSize(v[i]);
        return Result;
    }
}
 
<< B.h >>
#include "cx.h"  // defines CCX
 
namespace Ser
{
    size_t StreamSize(const CCX *p); // definition in
B.cpp
}
 
<< C.cpp >>
#include "A.h"    // Line (1)
#include "B.h"    // Line (2)
 
using namespace Ser;
using namespace std;
 
int main(int argc, char *argv[])
{
    vector<CCX *> v;
    size_t Size = StreamSize(v);
}
 
------
 
The returned error is "No matching function for call
to StreamSize(CCX *const&). If I swap line (1) with
line (2) in C.cpp, or remove the namespace definition
in B.cpp, the code compiles without any problem.
Besides, the above code used to work fine with gcc
2.96 but no longer works once I upgraded the gcc to
4.1.1.
 
If I don't swap the two lines in C.cpp, is there any
other way to resolve this? Thanks.
 
Regards,
Milan.



More information about the Gcc-help mailing list