0

I have a C++ project in VS 2015. I created a template function in one class.

TressPos.h

class TressPos
{

   //constructor
   //destructor

   template <typename T>
   void GetNewVector(T* t, int scale);
}

TressPos.cpp

template <typename T>
void TressPos::GetNewVector(T* t, int scale)
{
  //Do somethings here..
  //....................
}

Now in another class.

DigitalMaster.cpp

TressPos tp;

void DigitalMaster::NesterLock()
{
   tp.GetNewVector(this, 1200);
   //More todo's here...
   //................
   tp.GetNewVector(.....);
}

Now when i build the project, it's giving an linker error.

Basically complaining that the GetNewVector can't accept a pointer to DigitalMaster as the 1st argument.

So what i did was to move the template function as inline in TressPos class.

TressPos.h

class TressPos
{

   //constructor
   //destructor

   template <typename T>
   inline void TressPos::GetNewVector(T* t, int scale)
   {
      //Do somethings here..
      //....................
    }
}

Now i don't get any linker error when i call this template function in DigitalMaster class.

But for some reason, visual studio thinks there's no function called in GetNewVector in TressPos class. Even though both compiler and linker are happy.

i.e., in visual stuido editor, it draws a red line below the GetNewVector fuction in DigitalMaster.cpp file.

Any ideas what's happening? Am new to using templates in C++.

  • Your template function implementations should be in the header if you plan on using them outside of the cpp file you defined them. – drescherjm Feb 13 '19 at 02:42
  • ***draws a red line below*** Don't always trust the red squiggles. Intellisense will have false positives. – drescherjm Feb 13 '19 at 02:43
  • Possible duplicate of: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – P.W Feb 13 '19 at 04:23
  • 1
    @drescherjm, - Does that mean, I can ignore the red underlines given by visual studio editor to GetNewVector function? –  Feb 13 '19 at 11:38
  • Sometimes you have to ignore the squiggles and go with what the compiler says. – drescherjm Feb 13 '19 at 12:29
  • believe me no redline in visual studio with similar code. – Santosh Dhanawade Feb 13 '19 at 12:37

0 Answers0