0

following situation: I have in one dll a template class Point

namespace Image
{
     template<typename T> class Point
     {
     .
     .
     .

and tring to use this class in another dll. The class looks like:

//Base.h
template<typename T> class Point;

class Base{
    Point<double> _Point;
};

//Child.h
#include "Base.h"
class Child : public Base{
    Child(Point<double> pt);
    doSth();
}

//Child.cpp
#include "Child.h"
#include "Point.h"

Child::Child(Point<double> pt){
    _Point = pt;                   
}
Child::dosth(){
    Point<double> p  = _Point;  // In this Row i get an "undefined type 'Point<double>' Error
}

Any ideas why i get the error? is my idea totally wrong to forward declare the Point-Class in the header file and make the include in the .cpp ?

Thank you very much, have a nice day!

  • 1
    Unrelated to your problem, but don't use symbols with a leading underscore followed by an upper-case letter (like `_Point`). Those symbols are reserved in all scopes. [See this questions and answers for more details](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – Some programmer dude Feb 07 '17 at 14:29
  • As for your problem, isn't it `Image::Point<...>` (you're missing the namespace)? – Some programmer dude Feb 07 '17 at 14:30
  • sure just forget. In both cpp i have using namespace Image; – NeumannM Feb 07 '17 at 14:33
  • That's one reason why it's so important to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Feb 07 '17 at 14:43

1 Answers1

0

With the forward declaration you have in Base.h it doesn't matter if you used using namespace Image; first, the template class you declare in Base.h is still in the global namespace and not in the Image namespace. And that declaration will take precedence over the one in the Image namespace.

So there are really two solutions here: Either explicitly use Image::Point, or remove the forward declaration in Base.h (and include the header file where Image::Point<> is defined).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621