samedi 28 avril 2018

How to call a template function in a non-template class [duplicate]

This question already has an answer here:

  • I am trying to a make a generic function that displays vector elements of any data type by using templates.
  • I was successful in making a function template that displays the vector elements for any data type vector
  • But when I put the same function template inside a non-template class and try to call it then I get an error
  • What is the right way to call a template function inside a non-template class

main.cpp

#include <iostream>
#include <Eigen>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "tools.h"
using namespace std;

template <typename T>
void disp_vect(const vector<T> & vect)
{
  typename vector<T>::const_iterator it;
  for (it = vect.begin() ; it != vect.end(); ++it)
  {
    std::cout << ' ' << *it;
  }
  std::cout << '\n';
}


int main()
{
  Tools tools;
  vector<int> vect{ 10, 20, 30 };
  disp_vect(vect);    // No Error Here

  // ===================Below is what i tried to call ======================
  // tools.disp_vect(vect);  // <==== If used this then Error
  // tools.disp_vect<int>(vect);  // <==== If used this then Error
  // tools.disp_vect(vect);  // <==== If used this then Error
  // tools.template disp_vect(vect);  // <==== If used this then Error
  // tools.template disp_vect<int>(vect);  // <==== If used this then Error
  // ===================Above is what i tried to call ======================

}

tools.h

#ifndef TOOLS_H_
#define TOOLS_H_
#include <vector>
#include "Eigen"

using Eigen::MatrixXd;
using Eigen::VectorXd;
using namespace std;

class Tools {
public:
  /**
  * Constructor.
  */
  Tools();

  /**
  * Destructor.
  */
  virtual ~Tools();

  template <typename T>
  void disp_vect(const vector<T> & vect);

};

#endif /* TOOLS_H_ */

tools.cpp

#include <iostream>
#include "tools.h"
#include <Eigen>

using Eigen::VectorXd;
using Eigen::MatrixXd;
using std::vector;

Tools::Tools() {}

Tools::~Tools() {}


template <typename T>
void Tools::disp_vect(const vector<T> & vect)
{
  typename vector<T>::const_iterator it;
  for (it = vect.begin() ; it != vect.end(); ++it)
  {
    std::cout << ' ' << *it;
  }
  std::cout << '\n';
}

- I also tried replacing the typename key-word with class and still i get error - This is the error message that i get:

    undefined reference to `void Tools::disp_vect<int>(std::vector<int, std::allocator<int> > const&)'
    collect2.exe: error: ld returned 1 exit status`

  • I referred these links but these links did not solve my error and hence posting this question over here:

1] Function template in non-template class

2] How to call a template member function?

3] Why calling template member functions gives error?

  • I also referred many other links which I have not mentioned

  • Based on the above links I tried the following solution:

     // tools.disp_vect(vect);  // <==== If used this then Error
     // tools.disp_vect<int>(vect);  // <==== If used this then Error
     // tools.disp_vect(vect);  // <==== If used this then Error
     // tools.template disp_vect(vect);  // <==== If used this then Error
     // tools.template disp_vect<int>(vect);  // <==== If used this then Error
    
    
  • I am new to C++ so before you down-vote my question please comment what should I add more to make the question more clear

  • If this is a duplicate then please mark it duplicate with right answer but do not down-vote it .. I searched for solution and also tried so please do not down-vote... Thank-you

Aucun commentaire:

Enregistrer un commentaire