c++结构错误
C++ struct error
struct Node{
int ID, power, parent;
vector<int> connectedNodes;
Node(int ID_arg, int power_arg){
ID = ID_arg;
power = power_arg;
parent = -1;
}
};
struct Graph{
int rootID;
map<int, Node> nodes;
map<int,int> subtreeSizes;
Graph(){
rootID = 1;
nodes[1] = new Node(1,0);
}
};
我现在一定是犯了严重的错误,因为我不知道出了什么问题。它不喜欢我在节点映射中放置节点的方式
这是因为你有一个类型不匹配,如果你发布编译错误,这将是明显的:
nodes[1] = new Node(1,0);
^^^^^^^^ ^^^^^^^^^^^^^
Node& Node*
你可能想这样做:
nodes[1] = Node(1, 0);
在这种特殊情况下不起作用,因为Node
不是默认可构造的,而map::operator[]
需要这样做。无论如何,以下选项都可以工作:
nodes.insert(std::make_pair(1, Node(1, 0));
nodes.emplace(std::piecewise_construct,
std::forward_as_tuple(1),
std::forward_as_tuple(1, 0));
// etc.
相关文章:
- 创建结构的数组时遇到分段错误
- 使用具有结构不完整类型错误的模板
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 导致Segmentation错误的结构中的矢量
- 编译器指出结构和 int 说明的错误 2059
- 当结构位于单独的头文件中时'unregistered datatype'在 QML 中出现Q_GADGET错误
- 在 C 函数中返回结构会导致分段错误
- C++ 结构错误"调用'erase'没有匹配函数
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 结构中的错误"Incomplete type is not allowed"
- 体系结构x86_64的未定义符号:链接器错误
- 在另一个结构中声明内部结构会导致错误:结构使用无效
- 如何编写一个错误结构,该结构可以包含不同的强键枚举作为错误代码
- 错误:结构的初始值设定项太多
- 带有结构和指针的程序错误(结构,C++)
- 错误:结构的正向声明
- 使用结构体作为参数:错误:结构体未定义
- 定义错误结构体中的枚举
- "Was not declared in this scope"错误结构定义。C++
- GCC中可能的错误:结构中的文字字符串类型