-2

caller below when linking saids undefined reference...

in base.h file:

class tempc {
    public:
    int a;
} ;
class base                 // base class
{
    public:                // public
    template<T> int func(T*);     //  template defined here
};

in base.cpp file:

template<T>
int base :: func(T*)
{
    std::cout << "base::func called" << std::endl;
    return 0;
}

in derived.cpp file

class derived : public: base    // class defined
{
     void  caller()
    { 
        tempc a;
        func<tempc>(&a);    // template used here
        base::func<tempc>(&a);
    }
};
int main()
{
    derived d;
    d.caller();
}

the error is : undefined reference to `void base::func(tempc*)'

base is the base class

derived is the derived class from base

this caller saids undefined reference...

//sorry,because my source code is reaaaaly too large to show up

Anything
  • 1
  • 2

1 Answers1

0

The code works ok (after you correct the nonsense syntax):

class base
{
    public:
    template<class T> int func();
    //       ^^^^^
    //       use class keyword

}; // <-- semicolon here

template<class T>
//       ^^^^^
//       use class keyword
int base::func()
{
    return 0;
}

class derived : public base
//                    ^
//                    no colon here
{
    void  caller()
    { 
        func<int>(); // it works
        base::func<int>();    // this works too
    }
}; // <-- semicolon here
bolov
  • 72,283
  • 15
  • 145
  • 224
  • it does not work...but I copy the implementation to derived class,it works... – Anything Sep 16 '16 at 12:38
  • What do you mean it does not work. That's the most unhelpfull diagnostic ever. Look here, it works: http://ideone.com/b0mLVI – bolov Sep 16 '16 at 12:41
  • This answer's code [works](http://coliru.stacked-crooked.com/a/5cf53a8256f1f62b). If yours doesn't, **show us** (by editing the question) in what way yours differs. – Angew is no longer proud of SO Sep 16 '16 at 12:41
  • @Anything I have a feeling I should have left the question closed with the dupe. Did you even read up there? – πάντα ῥεῖ Sep 16 '16 at 12:44
  • @Anything we aren't telepathic. We can't know what is your real code. We can only see the code you post here. And it looks like the code you post here is not the same you are trying on. Moreover, we can't possibly know what the error you are getting is (since the code we have here it works). You are wasting everybody's time (**including your own**) when you expect us to guess or consult our crystal ball. Please post the real code **and the errors you are getting**. We can't do anything **to help you** when all you give us is "it doesn't work". – bolov Sep 16 '16 at 12:53