您的位置:首页 > 移动开发 > IOS开发

Vector中作为全局变量时注意的问题(转)

2007-03-05 16:57 357 查看
 
[align=left] Vector中作为全局变量时注意的问题[/align]
案例:两个文件:
//Class.h====================================================
#ifndef CLASS_H
#define CLASS_H
#include <vector>
class Node;
class Node
{
public:
    void Register()
    {
        Node::nodeCollection.push_back(this);
    }
    public:
    static std::vector<Node*> nodeCollection;
};
#endif

//main.cpp =====================================================
#include "Class.h"
#include <iostream>
using namespace std;
 
                           答案:即此处添加 std::vector<Node*> Node::nodeCollection;

int main()
{
Node aNode;
Node* pNodeA = new Node();
pNodeA->Register();
Node* pNodeB = new Node();
pNodeB->Register();
for (int i = 0; i < Node::nodeCollection.size(); i++)
{
if (Node::nodeCollection[i] != NULL)
{
delete Node::nodeCollection[i];
Node::nodeCollection[i] = NULL;
}
}
cout << "done!" << endl;
cin.ignore();
return 1;
}
//======================================================================
编译时出错,会提示error LNK2001unresolved external symbol****
正确回答:
在int main()之前加一句;
std::vector<Node*> Node::nodeCollection;
转自http://dev.cbw.com/c/c++/20055245604_4006740.shtml
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息