【发布时间】:2014-07-31 13:15:50
【问题描述】:
我有一个类,我只需要一个模板方法,像这样:
/* A.h */
class A {
public:
void foo() const;
private:
template <class T>
void foo2(const T& t, const std::string& s) {
/* */
}
}
这编译得很好,但是如果在 foo 专业化中我尝试调用 foo2 我会得到错误:
/* A.cpp */
void A::foo() {
this->foo2(1, "test");
}
错误是:
passing ‘const A’ as ‘this’ argument of ‘void A::foo2(const T&, const
string&) [with T = int, std::string = std::basic_string<char>]’
discards qualifiers [-fpermissive]
【问题讨论】:
-
错误是...?
-
@SteveTownsend "what T to infer from the literal 1" - 好吧,它可以毫无问题地推断出
int,因为文字1的类型是int... -
没有
foo特化,因为它不是模板。你应该告诉我们错误是什么,我们通常不会在这里练习心灵感应 -
@SteveTownsend 不起作用..
-
您遇到了 const 正确性问题,并且缺少部分错误消息。 SSCCE,请。
标签: c++ eclipse templates c++03