-1

I am implementing a Node class in C++ as an assignment.

The class :

#pragma once
#include <iostream>

class Node {
    size_t id;
    std::pair<size_t, Node> dist;
public:
    //Node();
    //Node(const size_t);
    Node(const size_t, const std::pair<size_t, Node>);
    Node(const Node&);
    //std::pair<const bool, const std::string> setId(size_t);
    //std::pair<const bool, const std::string> setDist(std::pair<size_t, Node>);
    //void operator=(const Node&);
};


Node::Node(const size_t nodeId, const std::pair<size_t, Node> distanceFromPreviousNode) : id(nodeId), dist(distanceFromPreviousNode) { }


Node::Node(const Node& node) {
    try {
        id = node.id;
        dist = node.dist;
        throw std::string("Could not set the id and distance to given value");
    }
    catch (std::string exception) {
        std::cerr << exception;
        exit(1);
    }
}

I assure that all the functions have been implemented in the node.cpp file.

However, I'm getting the following error std::pair<size_t, Node>::second uses undefined class 'Node'

I am completely clueless as to what the error wants me to do and why the intellisense is not detecting it. Oh, also the ctor is generating it (at least, that's the line number the error message points to)

kesarling He-Him
  • 1,944
  • 3
  • 14
  • 39
  • Please extract a [mcve] and provide that as part of your question. As it stands, your question is off-topic. – Ulrich Eckhardt May 02 '20 at 16:50
  • `std::pair` (or any other reference to `Node`, other than as a pointer or a reference) is not valid within the declaration of class `Node` because `Node` is an incomplete type at that point. You will have to consider another approach. Just what are you trying to achieve here? – Paul Sanders May 02 '20 at 16:58
  • Can I just copy your code to a file and compile it to reproduce the issues? – Ulrich Eckhardt May 02 '20 at 16:58
  • 1
    @PaulSanders, I'm trying to make a simple `copy ctor`. – kesarling He-Him May 02 '20 at 17:00
  • 1
    @d4rk4ng31 With your class as-is, you don't _need_ a copy constructor (or assignment operator - rule of zero). Anyway, what I can't wrap me head around is, what are all those `std::pair`s in there for? What is their purpose? – Paul Sanders May 02 '20 at 17:08
  • 1
    @PaulSanders, `size_t` is basically for the length, i.e. the distance (I know the variable name is a bit confusing; will change it) and `Node` is the node from which that distance is. It actually needs to be a vector (which I just realised) – kesarling He-Him May 02 '20 at 17:12
  • Can you use something like `std::pair`? That at least compiles, and should help you manage the object lifetime of the object stored in the `std::pair`. – Paul Sanders May 02 '20 at 17:18
  • @PaulSanders, Umm..will Node& instead of Node work? – kesarling He-Him May 02 '20 at 17:23
  • Well, it will compile, but there's a significant chance of a dangling reference somewhere down the line. It all depends on the object lifetime of the `Node` in the `std::pair` and how it is managed, and that's something you need to think about. – Paul Sanders May 02 '20 at 17:32
  • @PaulSanders, Got it. You were right :) and also, using Node& gives some weird error about a particular signature of std::pair::operator= being deleted. – kesarling He-Him May 02 '20 at 17:34
  • Yes, you can't copy a reference, so scratch that idea. That leaves you a choice between `std::shared_ptr` and a raw `Node *`. `std::shared_ptr` has nice reference counting semantics and should help you manage the lifetime of your various `Node`s. It is also copyable (unlike `std::unique_ptr`). Take a look. – Paul Sanders May 02 '20 at 20:09

1 Answers1

1
std::pair<size_t, Node> dist;

This member declaration requires that Node is fully defined at the point of declaration, which it isn't. This is what the error is telling you.

Also, what you are trying to do is impossible. What should the memory layout of an instance of Node look like? An instance of Node has a dist member, which itself has a member of Node type, which has a dist member, etc... This would require infinite memory.

It is allowed to have a Node* member in Node if that will help.

sparik
  • 1,181
  • 9
  • 16