【问题标题】:c++ constructor for template Node class模板Node类的c ++构造函数
【发布时间】:2021-03-31 20:25:29
【问题描述】:

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

最终目标:我希望能够制作这样的邻接列表。 "TUCSON" : ("PHOENIX" , 5), "LA" : ("TUC" , 15).
要么 0: (1, 5), 2: (0, 15); 注意 source 和 dest 有相同的类型,而 cost 总是数字。

当我运行以下代码时,我收到以下错误: 来自 main.cpp: "'Node' 的初始化没有匹配的构造函数"

这些是我遇到的构建时错误(为了便于阅读,将 gilepath 替换为 [path])

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;
    };

【问题讨论】:

  • 看起来Node&lt;int, int&gt; n0(0, { 1, 3 }); 正在尝试引用临时文件。 Node(T0 src, pair&lt;T0,T1&gt;&amp; data); 应该是 Node(T0 src, const pair&lt;T0,T1&gt;&amp; data);,但我不知道这能解决多少问题。
  • 请记住,您不必new C++ 中的所有内容。 In fact you're better off newing as little as possible.
  • 建议:获取并阅读a good C++ book or two。不要猜测 C++ 代码。 C++ 非常无情,很少用有效的代码奖励猜测。
  • 你不需要在任何地方都使用 new ,因为你已经在 Node 中定义了变量。对于示例,您可以尝试这些功能template &lt;typename T0, typename T1&gt; Node&lt;T0, T1&gt;::Node(T0 src,const pair&lt;T0,T1&gt;&amp; input_data){ // source = new T0(); source = src; // data = new pair&lt;T0,T1&gt;(); data = input_data; }
  • 我可以回答有效的解决方案,但不确定是否需要您在上面准确询问。

标签: c++ pointers templates graph constructor


【解决方案1】:

好吧,我实现了大家的建议,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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    相关资源
    最近更新 更多