【发布时间】:2020-04-13 10:44:07
【问题描述】:
为什么我得到错误“必须调用非静态成员函数的引用”:
<source>:35:19: error: reference to non-static member function must be called
list_.front().Set<Flags::First>(true);
~~~~~~~~~~~~~~^~~
<source>:40:10: note: in instantiation of member function 'List<int>::Set' requested here
list.Set();
^
<source>:13:8: note: possible target for call
void Set(bool value) {
^
1 error generated.
Compiler returned: 1
当我尝试用 clang 7.0.0 编译这段代码时?
#include <iostream>
#include <list>
using namespace std;
enum class Flags : uint8_t {
First = 1,
Second = 2
};
class Header {
public:
template <Flags flag>
void Set(bool value) {
flags_ = static_cast<Flags>(
value
? (static_cast<uint8_t>(flags_) | static_cast<uint8_t>(flag))
: (static_cast<uint8_t>(flags_) & (~static_cast<uint8_t>(flag))));
}
private:
Flags flags_{};
};
template <class T>
class List {
public:
void Set();
private:
std::list<Header> list_;
};
template <class T>
void List<T>::Set() {
list_.front().Set<Flags::First>(true);
}
int main() {
List<int> list;
list.Set();
return 0;
}
【问题讨论】:
-
无法复制。使用 g++ 9.3.1 或 clang++ 10.0.0 .
-
应该是
list_.front().template Set<Flags::First>(true); -
用clang++ 7-9转载
-
从删除“using namespace std”开始,它可能会更清晰。