错误:非法使用此类型作为表达式.C++
Error: illegal use of this type as an expression. C++
我在类 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 ©); //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
静态成员函数或静态数据成员只是整个类的一个副本,每个实例没有单独的副本。
因此,只能使用类名和::
运算符访问静态成员。
.
运算符仅与非静态成员或实例成员一起使用。
使用:
Node::create(1)
调用函数
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 提升精神:解析布尔表达式并简化为规范范式
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 使用正则表达式regex_search在字符串中查找字符串
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 概念中的cv限定符需要表达式参数列表
- 为什么constexpr的性能比正常表达式差
- 对于结构,表达式必须是可修改的ivalue
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 将fold表达式与std::一起用于两个元组
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- 标记 '","' 之前的预期主表达式
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 如何计算具有指定类型的表达式的相对精度和绝对精度
- 带有用户定义类的c++折叠表达式
- 即使使用调试编译标志,表达式也是"optimized out"
- holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- 这 4 个 lambda 表达式之间有什么区别?