我似乎做错了事,但不确定。这是我要执行的操作的最小示例:

#include <iostream>

using std::cout;

class CallMe {
 public:
  void Maybe() {
    cout << "A";
  }
};

class TemplateValue {
 public:
  static CallMe call_me;
};

template<typename T>
void CallMemberMember() {
  T::call_me.Maybe();
}

int main(int argc, char *argv[]) {
  CallMemberMember<TemplateValue>();
}

当我尝试构建它时,出现链接错误:
$ clang++ --std=c++11 repro_link_error.cc
Undefined symbols for architecture x86_64:
  "TemplateValue::call_me", referenced from:
      void CallMemberMember<TemplateValue>() in repro_link_error-9BE9gw.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我究竟做错了什么?

最佳答案

您需要提供静态成员call_me的定义:

CallMe TemplateValue::call_me;
int main(int argc, char *argv[]) {
  CallMemberMember<TemplateValue>();
}

10-08 11:00