Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++中实现我的图表有困难。_C++_Data Structures_Allocator - Fatal编程技术网

C++;数据结构分配器问题 我在C++中实现我的图表有困难。

C++;数据结构分配器问题 我在C++中实现我的图表有困难。,c++,data-structures,allocator,C++,Data Structures,Allocator,现在,我刚刚实现了顶点和边,下面是类: 顶点h #ifndef VERTEX_H_ #define VERTEX_H_ #include <string> #include <iostream> namespace MarcoGraphs { enum class Colors {black, red}; class Vertex { private: std::string id; int soglia, degree; double pe

现在,我刚刚实现了顶点和边,下面是类:

顶点h

#ifndef VERTEX_H_
#define VERTEX_H_
#include <string>
#include <iostream>

namespace MarcoGraphs {

enum class Colors {black, red};

class Vertex {
private:
    std::string id;
    int soglia, degree;
    double peso;
    Colors visited;
public:
    Vertex(std::string id);
    Vertex(const Vertex& param);
    Vertex& operator=(const Vertex& param);
    Vertex(Vertex&& param);
    Vertex& operator=(Vertex&& param);
    ~Vertex();
    bool operator!=(const Vertex& param);
    bool operator==(const Vertex& param);
    bool operator<(const Vertex& param);
    std::string getId();
    void setDegree(int degree);
    int getDegree();
    void incrementDegree();
    void setSoglia(int soglia);
    int getSoglia();
    void setVisited(Colors mycolors);
    Colors getVisited();
    void setPeso(double peso);
    double getPeso();
    friend std::ostream& operator<<(std::ostream& out, const Vertex& a);
};

} /* namespace MarcoGraphs */

#endif /* VERTEX_H_ */
#如果没有顶点#_
#定义顶点_
#包括
#包括
命名空间马可图{
枚举类颜色{黑色,红色};
类顶点{
私人:
std::字符串id;
int-soglia,学位;
双倍比索;
访问的颜色;
公众:
顶点(std::字符串id);
顶点(常数顶点和参数);
顶点和运算符=(常数顶点和参数);
顶点(顶点和参数);
顶点和运算符=(顶点和参数);
~Vertex();
布尔运算符!=(常数顶点和参数);
布尔运算符==(常数顶点和参数);
bool运算符seglia=soglia;
}
int Vertex::getSoglia(){
返回索格利亚;
}
void Vertex::setVisited(颜色mycolor){
这个->访问过的=mycolor;
}
颜色顶点::getVisited(){
回访;
}
无效顶点::塞特比索(双倍比索){
这->比索=比索;
}
双顶点::getPeso(){
归还比索;
}
std::ostream&operator
std::vector v(10);
这将创建一个包含10个元素的向量,但为此它需要一个默认构造函数,而您没有

您可以使用
reserve
来获得足够大的向量,或者将
顶点
传递给向量的构造函数

这里有一些参考:


Zsolt的答案是正确的,但可能没有完全回答您的问题。因此,我将尝试对此进行详细阐述

C++中,“默认构造函数”是一个不带参数的构造函数。

当你创建一个类,不定义任何构造函数时,C++为你创建一个默认构造函数,这可能是你还没有遇到这个问题的原因。在顶点的情况下,你已经创建了一些构造函数,所以编译器相信你已经明确地忽略了一个默认构造函数。存在gful默认值

当您拨打电话时:

std::vector<MarcoGraphs::Vertex> v(10);
std::vectorv(10);
您告诉编译器要构造一个包含10个顶点的向量。由于您没有提供分配器作为模板参数,因此它使用默认分配器,该分配器调用不存在的默认构造函数。这就是您看到的错误告诉您的(现在再读一遍,您应该能够理解它们的输出)

在主循环中,您使用push_back将顶点添加到向量中,因此实际上不需要构造10个默认顶点。您有几个选项:

  • 从v的定义中删除“(10)”,让编译器为您分配内存(本例中首选)
  • 创建一个有意义的默认构造函数(在这种情况下可能是错误的)
  • 遵循Zsolt的建议并保留内存
  • 保留内存实际上是一种优化,当你知道一个向量将存储多少个对象,并且不想用PASSPEBACK命令在内存中重新分配内存时,它是有用的。对于你的具体程序,我会认为这是一个过早的优化,并且只删除“(10)”。

    #ifndef EDGE_H_
    #define EDGE_H_
    #include <iostream>
    #include "Vertex.h"
    
    namespace MarcoGraphs {
    
    class Edge {
    private:
        Vertex* v1;
        Vertex* v2;
    public:
        Edge(Vertex v1, Vertex v2);
        Edge(const Edge& param);
        Edge& operator=(const Edge& param);
        Edge(Edge&& param);
        Edge& operator=(Edge&& param);
        ~Edge();
    
        bool operator!=(const Edge& param);
        bool operator==(const Edge& param);
        bool operator<(const Edge& param);
        Vertex* getV1();
        Vertex* getV2();
    
        friend std::ostream& operator<<(std::ostream& out, const Edge& a);
    };
    
    } /* namespace MarcoGraphs */
    
    #endif /* EDGE_H_ */
    
    #include "Edge.h"
    
    namespace MarcoGraphs {
    
    Edge::Edge(Vertex v1, Vertex v2):
        v1(&v1), v2(&v2){
    }
    
    Edge::~Edge() {
    }
    
    Edge::Edge(const Edge& param):
        v1(param.v1), v2(param.v2){
    }
    
    Edge& Edge::operator=(const Edge& param){
        v1 = param.v1;
        v2 = param.v2;
        return *this;
    }
    
    Edge::Edge(Edge&& param):
        v1(param.v1), v2(param.v2){
        param.v1 = nullptr;
        param.v2 = nullptr;
    }
    
    Edge& Edge::operator=(Edge&& param){
        v1 = param.v1;
        v2 = param.v2;
        param.v1 = nullptr;
        param.v2 = nullptr;
        return *this;
    }
    
    bool Edge::operator!=(const Edge& param){
        return !(*v1 == *(param.v1) && *v2 == *(param.v2));
    }
    
    
    bool Edge::operator==(const Edge& param){
        return (*v1 == *(param.v1) && *v2 == *(param.v2));
    }
    
    bool Edge::operator<(const Edge& param){
        if(*v1 != *(param.v1))
            return *v1 < *(param.v1);
        return *v2 < *(param.v2);
    }
    
    Vertex* Edge::getV1(){
        return v1;
    }
    
    
    Vertex* Edge::getV2(){
        return v2;
    }
    
    std::ostream& operator<<(std::ostream& out, const Edge& a){
        out << a.v1->getId() << "," << a.v2->getId();
        return out;
    }
    
    } /* namespace MarcoGraphs */
    
    #include "Vertex.h"
    #include "Edge.h"
    #include <iostream>
    #include <set>
    #include <algorithm>
    #include <vector>
    
    int main(){
        MarcoGraphs::Vertex v1("Marco");
        MarcoGraphs::Vertex v2("Stefano");
        MarcoGraphs::Vertex v3("Giovanni");
    
    
        std::vector<MarcoGraphs::Vertex> v(10);
        v.push_back(v1);
        v.push_back(v2);
        v.push_back(v3);
        return 0;
    }
    
    15:15:20 **** Incremental Build of configuration Debug for project Thesis ****
    make all 
    Building file: ../Main.cpp
    Invoking: Cross G++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"Main.d" -MT"Main.d" -o "Main.o" "../Main.cpp"
    In file included from /usr/include/c++/4.9.0/bits/stl_tempbuf.h:60:0,
                     from /usr/include/c++/4.9.0/bits/stl_algo.h:62,
                     from /usr/include/c++/4.9.0/algorithm:62,
                     from ../Main.cpp:12:
    /usr/include/c++/4.9.0/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = MarcoGraphs::Vertex; _Args = {}]’:
    /usr/include/c++/4.9.0/bits/stl_uninitialized.h:515:43:   required from ‘static void std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = MarcoGraphs::Vertex*; _Size = unsigned int; bool _TrivialValueType = false]’
    /usr/include/c++/4.9.0/bits/stl_uninitialized.h:570:33:   required from ‘void std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = MarcoGraphs::Vertex*; _Size = unsigned int]’
    /usr/include/c++/4.9.0/bits/stl_uninitialized.h:631:50:   required from ‘void std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = MarcoGraphs::Vertex*; _Size = unsigned int; _Tp = MarcoGraphs::Vertex]’
    /usr/include/c++/4.9.0/bits/stl_vector.h:1311:28:   required from ‘void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = MarcoGraphs::Vertex; _Alloc = std::allocator<MarcoGraphs::Vertex>; std::vector<_Tp, _Alloc>::size_type = unsigned int]’
    /usr/include/c++/4.9.0/bits/stl_vector.h:279:34:   required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = MarcoGraphs::Vertex; _Alloc = std::allocator<MarcoGraphs::Vertex>; std::vector<_Tp, _Alloc>::size_type = unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<MarcoGraphs::Vertex>]’
    ../Main.cpp:21:39:   required from here
    /usr/include/c++/4.9.0/bits/stl_construct.h:75:7: error: no matching function for call to ‘MarcoGraphs::Vertex::Vertex()’
         { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
           ^
    /usr/include/c++/4.9.0/bits/stl_construct.h:75:7: note: candidates are:
    In file included from ../Main.cpp:8:0:
    ../Vertex.h:27:2: note: MarcoGraphs::Vertex::Vertex(MarcoGraphs::Vertex&&)
      Vertex(Vertex&& param);
      ^
    ../Vertex.h:27:2: note:   candidate expects 1 argument, 0 provided
    ../Vertex.h:25:2: note: MarcoGraphs::Vertex::Vertex(const MarcoGraphs::Vertex&)
      Vertex(const Vertex& param);
      ^
    ../Vertex.h:25:2: note:   candidate expects 1 argument, 0 provided
    ../Vertex.h:24:2: note: MarcoGraphs::Vertex::Vertex(std::string)
      Vertex(std::string id);
      ^
    ../Vertex.h:24:2: note:   candidate expects 1 argument, 0 provided
    subdir.mk:24: recipe for target 'Main.o' failed
    make: *** [Main.o] Error 1
    
    std::vector<MarcoGraphs::Vertex> v(10);