0

我在类 Node 中创建了一个静态成员函数,并希望使用一些参数调用该函数。如何在我的主函数中调用该函数?

class Node;
typedef shared_ptr<Node> SharedNode;

class Node {
    Node* parent;
    vector< SharedNode > children;
    int value;

    //limiting construction
    Node(int a_value):value(a_value),parent(0){}
    Node(const Node &copy); //non-construction-copyable
    Node& operator=(const Node& copy); //non-copyable
public:
    static SharedNode create(int a_value){
        return SharedNode(new Node(a_value));
    }
    SharedNode addChild(SharedNode child){
        child->parent = this;
        children.push_back(child);    // First there is a typo here. (nodes.push_back     is incorrent)
        return child;
    }


int main(){

    SharedNode a1 = Node.create(1);
    SharedNode b1 = Node.create(11);
    SharedNode b2 = Node.create(12);
    SharedNode b3 = Node.create(13);
    SharedNode b21 = Node.create(221);
    a1.get()->addChild(b1);
    a1.get()->addChild(b2);
    a1.get()->addChild(b3);
    b2.get()->addChild(b21);
    b2.get()->getNode(221);


    int hold;
    cin>>hold;
}

它给了我一个错误:非法使用这种类型作为表达式。感谢你们!

4

3 回答 3

5

静态是使用:: 运算符访问的,而不是.应该这样Node::Create

于 2012-06-11T19:57:05.010 回答
1

Static member functions or Static Data members are just one copy for the whole class and no separate copies for each instances.

So static members can be accessed only with class name and :: operator.

. Operator is used with only non static members or instance members.

于 2012-06-12T05:48:03.313 回答
1

利用:

Node::create(1)

调用函数

于 2012-06-11T19:56:53.603 回答