我正在尝试编译教程中的代码,但我得到了ABI::CXX11 undefined reference error
。这是有问题的代码:
代码清单-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]'
从我看过/读过的内容来看,它与the difference between std::list and std::list 2011.有关。
从我所阅读的on an other stack post开始,使用-D_GLIBCXX_USE_CXX11_ABI=0
应该可以解决问题,但它根本没有改变任何内容(我尝试将其添加到C ++编译器中的其他选项)并在链接器中附加选项)。
答案 0 :(得分:1)
在Code-Listing-2 Node.cpp中:
#include "Node.hpp"
list<Node *> nodes;
Node::Node() {
// ...
应该是:
#include "Node.hpp"
list<Node *> Node::nodes;
Node::Node() {
// ...
如果没有Node::
,则声明与静态成员nodes
无关的全局变量Node::nodes
,因此未定义的引用因为静态成员变量需要正确的定义。