Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从与返回值类型匹配的映射返回值_C++_Constructor_Compiler Errors - Fatal编程技术网

C++ 从与返回值类型匹配的映射返回值

C++ 从与返回值类型匹配的映射返回值,c++,constructor,compiler-errors,C++,Constructor,Compiler Errors,我收到了以下错误 In file included from /Users/james/ClionProjects/United States Computing Olympiad/graphs.cpp:2: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439: In fil

我收到了以下错误

In file included from /Users/james/ClionProjects/United States Computing Olympiad/graphs.cpp:2:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1673:31: error: no matching constructor for initialization of 'Vertex'
            ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
文件包含在/Users/james/ClionProjects/United States Computing Olympiad/graphs.cpp:2:
在/Applications/Xcode.app/Contents/Developer/Toolchains/xcodefault.xctoolchain/usr/bin//包含的文件中/include/c++/v1/string:439:
在/Applications/Xcode.app/Contents/Developer/Toolchains/xcodefault.xctoolchain/usr/bin//包含的文件中/include/c++/v1/算法:628:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/./include/c++/v1/memory:1673:31:错误:没有用于“顶点”初始化的匹配构造函数
::新建((void*)\u p)\u向上(\u VSTD::forward(\u args)…);
以下是我的代码相关部分的摘要:

class Vertex {
 public:
    int label;
    vector<Vertex> adjacent_vertices;
    Vertex(const int l) : label(l) { }
    Vertex(const int l, vector<Vertex> adjacents) : label(l), adjacent_vertices(adjacents) { }
    Vertex(const Vertex& other_vertex) : label(other_vertex.label), adjacent_vertices(other_vertex.adjacent_vertices){ }
};
class Graph {
 public:
    unordered_map<int, Vertex> vertices;
    protected:
    Vertex getmake_vertex(const int v) {
        if (vertices.find(v) == vertices.end() ) {
            // not found, make new vertex
            vertices[v] = Vertex(v);
        }
        return vertices[v];
    };
};
类顶点{
公众:
int标签;
向量邻接顶点;
顶点(const int l):标签(l){}
顶点(常量int l,向量邻接):标签(l),相邻顶点(邻接){
顶点(常数顶点和其他顶点):标签(其他顶点.标签),相邻顶点(其他顶点.相邻顶点){}
};
类图{
公众:
无序映射顶点;
受保护的:
顶点getmake_顶点(常数int v){
if(顶点.find(v)=顶点.end()){
//未找到,请创建新顶点
顶点[v]=顶点(v);
}
返回顶点[v];
};
};

我已经确认,运行此命令时,如果所有其他内容都被注释掉,则会产生编译器错误。有人能向我解释为什么会发生这种情况,以及我如何解决它吗?是完整编译器输出的要点。

当你说
顶点[v]=顶点(v)
必须为键
v
创建一个
顶点
(在赋值之前),但是
顶点
没有默认构造函数

你应该使用的是
顶点。插入(成对(v,顶点(v))
甚至
顶点。放置(v,顶点(v))

这也适用于
返回顶点[v]。尽管您和我都知道,当这个返回语句被命中时,v总是有一个值,但是编译器没有并且仍然必须生成代码来生成一个值,这导致了一个错误


将其设置为
返回顶点。查找(v)->秒将修复该部分。不需要检查并确保
find
值不是
end
,因为如果它不在那里,我们就把它放进去

如果我正确阅读了错误消息的这一部分,则需要
顶点的默认构造函数
;特别是:

需要2个参数,但提供了0

/Users/james/ClionProjects/United States Computing Olympiad/graphs.cpp:14:5:注意:候选构造函数不可行:需要单参数“l”,但未提供任何参数
顶点(const int l):标签(l){}
^
/Users/james/ClionProjects/United States Computing Olympiad/graphs.cpp:16:5:注意:候选构造函数不可行:需要单个参数“其他顶点”,但未提供任何参数
顶点(常数顶点和其他顶点):标签(其他顶点.标签),相邻顶点(其他顶点.相邻顶点){}
^
/Users/james/ClionProjects/United States Computing Olympiad/graphs.cpp:15:5:注意:候选构造函数不可行:需要2个参数,但提供了0
顶点(常量int l,向量邻接):标签(l),相邻顶点(邻接){

使用
操作符[]
需要在您的情况下将
映射的类型(
顶点
)设置为默认可构造的1,因为如果键在映射中不存在,它会插入一个默认的-constructed1
映射的类型
。这是一个运行时决策,因此即使密钥确实存在,您仍然需要在编译时使用默认构造函数

在C++17中,使用:

否则,请使用或

(如果将
顶点(int)
构造函数
显式化,则可能需要使用
顶点(v)
,您可能应该这样做。)

如果关键点已经在地图中,则所有这些都不会实际插入。全部三个 返回一对
,迭代器指向具有指定键的元素



1不完全正确,但对于我们的目的来说已经足够正确。

该方法旨在创建顶点(如果顶点不存在),然后返回它。既然每个顶点都需要一个标签,我该如何创建一个默认构造函数呢?如果你的类没有默认的c'torYeah,那就好了。您仍然将其保存在
if(texts.find(v)…
块中。我正在使用参数
v
创建顶点,这是列出的第一个构造函数。使用任一选项替换
顶点[v]=顶点(v)
将导致编译器错误
/Users/james/ClionProjects/United States Computing Olympiad/graphs.cpp:14:5: note: candidate constructor not viable: requires single argument 'l', but no arguments were provided
    Vertex(const int l) : label(l) { }
    ^
/Users/james/ClionProjects/United States Computing Olympiad/graphs.cpp:16:5: note: candidate constructor not viable: requires single argument 'other_vertex', but no arguments were provided
    Vertex(const Vertex& other_vertex) : label(other_vertex.label), adjacent_vertices(other_vertex.adjacent_vertices){ }
    ^
/Users/james/ClionProjects/United States Computing Olympiad/graphs.cpp:15:5: note: candidate constructor not viable: requires 2 arguments, but 0 were provided
    Vertex(const int l, vector<Vertex> adjacents) : label(l), adjacent_vertices(adjacents) { }
Vertex getmake_vertex(const int v) {
    return vertices.try_emplace(v, v).first->second;
}
Vertex getmake_vertex(const int v) {
    return vertices.insert({v, v}).first->second;
}