首页 > 解决方案 > 模板Node类的c ++构造函数

问题描述

我正在尝试创建一个要在加权图中使用的节点类。定义如下,我试图尽可能具体,但如果您有任何问题,请告诉我

最终目标:我希望能够制作这样的邻接列表。“图森”:(“凤凰”,5),“洛杉矶”:(“图克”,15)。
或 0: (1, 5), 2: (0, 15); 注意 source 和 dest 有相同的类型,而 cost 总是数字。

当我运行以下代码时,我收到以下错误:来自 main.cpp:“No matching constructor for initialization of 'Node<int, int>'”

这些是我得到的构建时间错误(用 [path] 替换 gilepath 以提高可读性)

Semantic Issue Group
[path]List.h:23:5: Constructor for 'Node<int, int>' must explicitly initialize the reference member 'data'
[path]main.cpp:4:10: In file included from [path]main.cpp:4:
[path]main.cpp:9:19: In instantiation of member function 'Node<int, int>::Node' requested here
[path]List.h:20:19: Declared here
[path]List.h:47:20: Assigning to 'int' from incompatible type 'int *'; dereference with *
[path]main.cpp:4:10: In file included from [path]main.cpp:4:
[path]List.h:49:16: No viable overloaded '='
[path]main.cpp:4:10: In file included from [path]main.cpp:4:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:512:11: Candidate function not viable: no known conversion from 'pair<int, int> *' to 'const typename conditional<is_copy_assignable<first_type>::value && is_copy_assignable<second_type>::value, pair<int, int>, __nat>::type' (aka 'const std::__1::pair<int, int>') for 1st argument; dereference the argument with *
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:525:11: Candidate function not viable: no known conversion from 'pair<int, int> *' to 'typename conditional<is_move_assignable<first_type>::value && is_move_assignable<second_type>::value, pair<int, int>, __nat>::type' (aka 'std::__1::pair<int, int>') for 1st argument; dereference the argument with *
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:334:62: Candidate template ignored: disabled by 'enable_if' [with _Tuple = std::__1::pair<int, int> *]
    template <typename T0, typename T1>
    class Node{
    public:
        T0 source; //type could be an int specifying the numerical index of source
        //or a string, specifying the alphanumerical name of source.
        
        pair<T0, T1> &data; //data has the form (dest, cost), where source and dest are of same type (ie both or int or both string)
        //cost is always a int or float;
        Node<T0, T1>* next = nullptr;
        Node(T0 src, pair<T0,T1>& data); //paramerized c-tor
        Node();
        ~Node();
        Node(const Node<T0, T1>& orig_Node); //copy c-tor
    
    };
    template <typename T0, typename T1>
    //default constructor
    Node<T0, T1>::Node(){
        source = 0;
        data = {0,0};
    }
    
    template <typename T0, typename T1>
    Node<T0, T1>::Node(const Node<T0, T1>& orig_Node){
        T0 source_copy = new T0();
        *source_copy = orig_Node.source;
        pair<T0,T1>* data_copy = new pair<T0, T1>();
        *data_copy = *(orig_Node.data);
        Node<T0, T1>*next = orig_Node.next;
    }
    
    template <typename T0, typename T1>
    Node<T0, T1>::Node(T0 src, pair<T0,T1>& input_data){
        source = new T0();
        source = src;
        data = new pair<T0,T1>();
        this->data = input_data;
    }

    int main(int argc, const char * argv[]) {
        Node<int,int> n0(0, {1,3});
        cout << "n0: " << "(" << n0.data.first << "," << n0.data.second << ")" << endl;
        
        int a[5] = {1,2,3,4};
        
        
       return 0;
    };

标签: c++pointerstemplatesgraphconstructor

解决方案


好吧,我实现了大家的建议,Node 的数据成员不应该用“new”初始化,我可以在 main.js 中构造一个节点。谢谢你的帮助!

#ifndef ECE275LIB_CONTAINERS_LIST_H
#define ECE275LIB_CONTAINERS_LIST_H
#include <stdio.h>
#include <cstdlib>
#include <iostream>

using std::cout;
using std::endl;
using std::pair;

using uint = unsigned int;

template <typename T0, typename T1>
class Node{
public:
    T0 source; //type could be an int specifying the numerical index of source
    //or a string, specifying the alphanumerical name of source.
    
    pair<T0, T1> data; //data has the form (dest, cost), where source and dest are of same type (ie both or int or both string)
    //cost is always a int or float;
    Node<T0, T1>* next = nullptr;
    Node();
   // ~Node();
    Node(const Node<T0, T1>& orig_Node);
    Node(T0 src, pair<T0,T1> data); //paramerized constructor

};
template <typename T0, typename T1> //default c-tor
//default constructor
Node<T0, T1>::Node(){
    source = 0;
    data = {0,0};
}

template <typename T0, typename T1>  //copy c-tor
Node<T0, T1>::Node(const Node<T0, T1>& orig_Node){
    T0 source_copy = orig_Node.source;
    pair<T0,T1> data_copy = orig_Node.data;
    Node<T0, T1>*next = orig_Node.next;
}

template <typename T0, typename T1> //paramerized c-tor
Node<T0, T1>::Node(T0 src, pair<T0,T1> input_data){
    source = src;
    data = input_data;
}

推荐阅读