-1

I am trying to create a node class to be used in a weighted graph. definition is below, I tried to be as specific as possible, but please let me know if you have any questions

end goal: I want to be able to make an adjacency list like so. "TUCSON" : ("PHOENIX" , 5), "LA" : ("TUC" , 15).
or 0: (1, 5), 2: (0, 15); note how source, dest have the same type, while cost is always numeric.

When I run the below code, I get the following errors: from main.cpp: "No matching constructor for initialization of 'Node<int, int>'"

these are the build time errors I am getting (replaced the gilepath with [path] for readability)

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;
    };
Rahel Miz
  • 103
  • 8
  • 1
    Looks like `Node n0(0, { 1, 3 });` is trying to take a reference to a temporary. `Node(T0 src, pair& data);` should probably be `Node(T0 src, const pair& data);`, but I don't know how much this will fix. – user4581301 Dec 22 '20 at 05:35
  • 1
    Remember that you don't have to `new` everything in C++. [In fact you're better off `new`ing as little as possible](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). – user4581301 Dec 22 '20 at 05:38
  • Suggestion: Get and read [a good C++ book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Don't guess at C++ code . C++ is pretty much unforgiving and rarely rewards guesses with code that works. – user4581301 Dec 22 '20 at 05:48
  • You don't need to use new everywhere as you have defined the variable in `Node` . For sample you can try these function `template Node::Node(T0 src,const pair& input_data){ // source = new T0(); source = src; // data = new pair(); data = input_data; }` – amar_1995 Dec 22 '20 at 06:09
  • I can answer the working solution but not sure if want you exactly asked above. – amar_1995 Dec 22 '20 at 06:11
  • Please don't post images of code or errors, post text instead. Also, you don't want this as a data member, ever: `pair &data;` Drop the `&`. – n. 'pronouns' m. Dec 22 '20 at 06:51
  • thanks for the information! would one of you be able to re-create the problem please and let me know what you find? – Rahel Miz Dec 22 '20 at 15:39

1 Answers1

0

alright I implemented everyone's suggestion that Node's data members should not be initialized with "new" and I am able to construct a node in main. Thanks for your help!

#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;
}
Rahel Miz
  • 103
  • 8