Type mismatch using template in C++
I've written a class template (in the file abc.hpp
):
template <class Node>
class Edge {
public:
Edge(Node n1, Node n2):_start(MIN(n1,n2)),_end(MAX(n1,n2)) {};
Node GetStart ()const {return _start;}
Node GetEnd ()const {return _end;}
friend std::ostream& operator<<(std::ostream& os, const Edge<Node>& ed)
{
os << "(" << ed._start << ", " << ed._end << ")";
return os;
}
protected:
Node _start;
Node _end;
};
And I've written a test program:
#include <iostream>
#include "abc.hpp"
int main (){
Edge<int> my_var (1,2);
Edge<int> my_var2 (1,3);
int vstart = my_var.GetStart;
int vend = my_var.GetEnd;
cout << my_var << " : " << my_var2 << endl;
}
When I try to compile it (using devcpp) the compiler puts out an error message regarding the lines int vstar开发者_高级运维t = my_var.GetStart;
and int vend = my_var.GetEnd;
argument of type `int(Edge<int>::)() const` does not match `int`
What is the problem, and how do I fix it?
You're trying to assign the function to vstart, instead of the result of calling the function:
int vstart = my_var.GetStart(); // <- I added () here
int vend = my_var.GetEnd(); // <- and here.
精彩评论