错误:非法使用此类型作为表达式。C++
我在类节点中创建了一个静态成员函数,并希望使用一些参数调用该函数。如何在主函数中调用该函数错误:非法使用此类型作为表达式。C++,c++,inheritance,static,C++,Inheritance,Static,我在类节点中创建了一个静态成员函数,并希望使用一些参数调用该函数。如何在主函数中调用该函数 class Node; typedef shared_ptr<Node> SharedNode; class Node { Node* parent; vector< SharedNode > children; int value; //limiting construction Node(int a_value):value(a_va
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 ©); //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;
}
它给了我一个错误:非法使用此类型作为表达式。
谢谢你们 使用:
Node::create(1)
要调用函数可以使用::not访问静态。因此,应该是Node::Create静态成员函数或静态数据成员对于整个类只有一个副本,对于每个实例没有单独的副本 因此,只能使用类名和::运算符访问静态成员
。运算符仅用于非静态成员或实例成员 请注意:从您的代码来看,使用唯一的\u ptr而不是共享的\u ptr更有意义:没有共享所有权。不过,这需要对代码的用法稍作更改。在将节点添加为子节点之前,您无法创建节点。非常感谢。我忘了这个语法。