在 C++ 中使用模板时类型不匹配
我已经编写了一个类模板(在文件 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;
};
并且我编写了一个测试程序:
#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;
}
当我尝试编译它(使用 devcpp)时,编译器会发出有关以下内容的错误消息: int vstart = my_var.GetStart;
和 int vend = my_var.GetEnd;
argument of type `int(Edge<int>::)() const` does not match `int`
行有什么问题,如何解决?
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 vstart = 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试将函数分配给 vstart,而不是调用函数的结果:
You're trying to assign the function to vstart, instead of the result of calling the function: