未定义对 'ClassName::memberField[abi:cxx11]' 的引用

4
我正在尝试从教程中编译代码,但是我得到了一个ABI::CXX11未定义引用错误。 这里是有问题的代码:

代码清单1 Node.hpp:

#ifndef NODE_HPP
#define NODE_HPP

#include "Node.hpp"
#include <list>
using std::__cxx11::list;//I am forced to use std-c++11 because of wxWidgets 3.0.2
#include <cstdlib>

class Node {
public:
    Node();
    Node(const Node& orig);
    virtual ~Node();

    void setParent(Node * parent);
    Node * getParent();

    list<Node *> &getChildren();
    static list<Node *> &getNodes();

private:
    static list<Node *> nodes;
protected:
    list<Node *> children;
    Node * parent;

};

#endif /* NODE_HPP */

代码清单2 Node.cpp:
#include <cstdlib>

#include "Node.hpp"

list<Node *> nodes;

Node::Node() {
    parent = NULL;

    nodes.push_back(this);//line 23
}

Node::Node(const Node& orig) {
}

Node::~Node() {
    nodes.remove(this);//line 30
}

void Node::setParent(Node * p){
    if( p == NULL && parent != NULL ) {
        parent->children.remove(this);
        parent = NULL;
    } else if( p->getParent() != this ) {
        parent = p;

        parent->children.push_back(this);
    }
}

Node * Node::getParent(){
    return parent;//line 53
}

list<Node *> &Node::getChildren(){
    return children;
}

list<Node *> &Node::getNodes(){
    return nodes;
}

这是我收到的错误信息:
build/Debug/MinGW-Windows/Node.o: In function `ZN4NodeC2Ev':
E:\Projects\CPP\wxWidgets/Node.cpp:23: undefined reference to `Node::nodes[abi:cxx11]'
build/Debug/MinGW-Windows/Node.o: In function `ZN4NodeD2Ev':
E:\Projects\CPP\wxWidgets/Node.cpp:30: undefined reference to `Node::nodes[abi:cxx11]'
build/Debug/MinGW-Windows/Node.o: In function `ZN4Node8getNodesB5cxx11Ev':
E:\Projects\CPP\wxWidgets/Node.cpp:53: undefined reference to `Node::nodes[abi:cxx11]'

根据我所看/读到的,这似乎与 std::list 与 std::list 2011 之间的区别有关。
根据我在其他堆栈帖子中所读到的,使用-D_GLIBCXX_USE_CXX11_ABI=0应该可以解决此问题,但实际上没有任何变化(我尝试将其添加到C++编译器附加选项和链接器附加选项中)。
1个回答

2
在 Code-Listing-2 Node.cpp 文件中:
#include "Node.hpp"

list<Node *> nodes;

Node::Node() {
   // ...

Should be:

#include "Node.hpp"

list<Node *> Node::nodes;

Node::Node() {
   // ...

如果没有使用 Node::,你声明的全局变量 nodes 与静态成员变量 Node::nodes 没有关系,因此会出现未定义引用的错误,因为静态成员变量需要正确的定义。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接