1

Sorry if this is a duplicate.
Here's one piece of example code that I cannot understand about static pointer class member.

#include <iostream>

class MyField {
 public:
  MyField() { std::cout << "ctr for MyField\n"; }
  ~MyField() { std::cout << "dtr for MyField\n"; }
  friend std::ostream &operator<<(std::ostream &os, MyField field) {
    os << "dump for MyField with addr: " << &field << std::endl;
    return os;
  }
};

class MyClass {
 private:
  static MyField field_;

 public:
  static MyField *p_field_;
  static void setField(MyField const &field) { field_ = field; }
  static void setPField(MyField *p_field) { p_field_ = p_field; }
  static void dumpField() { std::cerr << "field_: " << field_; }
  static void dumpPField() { std::cerr << "p_field_: " << *p_field_; }
  MyClass() { std::cout << "ctr for MyClass\n"; }
  ~MyClass() { std::cout << "dtr for MyClass\n"; }
};

int main(void) {
  MyClass::setPField(NULL);  /// undefined reference to MyClass::p_field_
  MyClass::p_field_ = NULL;  /// undefined reference to MyClass::p_field_
  MyField field;
  MyClass::setField(field);    /// fine
  MyClass::p_field_ = &field;  /// undefined reference to MyClass::p_field_
  MyClass::setPField(&field);  /// undefined reference to MyClass::p_field_
  MyClass();
  MyClass::dumpField();   /// fine
  MyClass::dumpPField();  /// fine
  return 0;
}

For class MyClass, why the set for Field field_(setField) works but not for p_field_(setPField)? And it still works for the access to address of p_field_(dumpPField)?

BTW, I tried gcc4.8/clang3.4 with or without --std=c++11.

Hongxu Chen
  • 5,240
  • 2
  • 45
  • 85
  • Duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?: `static` data members must be defined outside the class in a single translation unit](http://stackoverflow.com/a/12574407/902497) – Raymond Chen Jul 26 '14 at 13:47
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – mkaes Jul 31 '14 at 15:32

1 Answers1

4

Add:

MyField * MyClass::p_field_ = NULL;

outside of any function, e.g. just before int main() {

M.M
  • 138,810
  • 21
  • 208
  • 365
Zyend
  • 572
  • 1
  • 4
  • 24
  • I guess you mean `Myclass::p_field_ = NULL;` in function `main`, but it doesn't work either. However I'll add this into the source code to make the question more clear. – Hongxu Chen Jul 26 '14 at 11:46
  • 2
    No, try doing what he wrote at namespace (global) scope. – Ulrich Eckhardt Jul 26 '14 at 12:10
  • He means `MyField *MyClass::p_field;` outside of `main`. (The `= NULL` is redundant since static pointers' default initialization is to a null pointer) – M.M Jul 26 '14 at 14:07
  • 1
    You will also need `MyField MyClass::field_;` , outside of `main`. – M.M Jul 26 '14 at 14:12
  • @MattMcNabb Wow, that works. But why no definition of `MyField MyClass::field_` actually compiles and runs without problems? – Hongxu Chen Jul 27 '14 at 01:28
  • @MattMcNabb And can you give a detailed explanation as an answer please? – Hongxu Chen Jul 27 '14 at 01:30