Home Creating template function object in a class template
Reply: 0

Creating template function object in a class template

user2244
1#
user2244 Published in April 22, 2018, 3:06 am

In my previous question, I was enquiring about the argument type of a class template in other class template.

In short, in class I can call a member function either by creating an object and calling upon it OR by directly using a scope resolution operator. Now, I want to use it in following 2 class templates. First class template is:

template<typename T>
class Point {
  public:
      //constructor
      Point () { }

      //initialise the vector 
      Point(std::vector<T> &vector);

      //deconstructor
      ~Point () { }

      //to calculate distance between two points
      T distance(const Point &p1, const point &p2);

      //members
      std::vector<T> data_points;
      int dimensions;
      int name;
      };

And the second class template is:

template<typename T> 
class calculate {
public:
  bool initialise(const std::vector<Point<T>> &points);

  bool load_points(const std::string &filepath, std::vector<Point<T>> *dpt);

  const std::vector<Point<T>> &getPoints() const 
      {
        return points;
      }

  const std::vector<Point<T>> &getMeans() const 
      {
        return means;
      }

private:
  std::vector<Point<T>> means;
  std::vector<Point<T>> points;
};

Following is a piece of "main.cc" :

//inside int main()

int main(int argc, char *argv[])
{
...
...
std::vector<Point<double>> points;

calculate<double>::load_points(filepath, &points); //error shows in this line

for(const auto &p : points)
{
  std::cout << p << std::endl;
}
...
...
...
return 0;
}

This method is working completely fine in normal class implementations but as soon as I try to use it with templates, it shows the following error:

error: cannot call member function 
'bool calculate<T>::load_points(const string&, std::vector<Point<T>> *) 
[with T = double; std::__cxx11::basic_string <char>]' 
without object

Why does it work with classes but not with class templates ?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303945 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO