0

Sorry in advance for what may be a bad post. I've scoured stackoverflow for pre existing posts that answer my question, but although many posts on here are similar, none of them seem to apply to my situation. I have struct Node, designed to be a container. Class Characters, which has character's name and power. For some reason without the default constructor the code crash with error:

enter image description here

#include <sstream>
#include <string>
#include <vector>
#include <iostream>
class Characters {
    public:
        int power;
        std::string name;
        Characters(int power, const std::string &name) : power(power), name(name) {}
        // Characters() : power(0), name("") {}
        //without above line, the code crashes with error: no matching function for call to 'Characters::Characters()' 39 |     Node (const Node<T> &other) {

        Characters(const Characters &other) {
            power=other.power;
            name = other.name;
        }

};

template <typename T>
struct Node {
    T val;
    Node (const T &val) : val(val) {}
    Node (const Node<T> &other) {
        val= other.val;
    }
};

int main() {
    using namespace std;
    Node<Characters> n1(Characters(4, "meh"));
    Node<Characters> n2=n1;

    return 0;
}

I have no idea why this occurs without default constructor. I'm probably just bad at using google, but none of the things I search up seems to address my issue.

Any help would be greatly appreciated. Thanks!

11
  • 1
    Why should i not upload images of code/data/errors? Commented May 15, 2023 at 9:22
  • 2
    you are using the member initializer list for one constructor, why not for the other? Members are initialized before the body of the constructor is executed, in absence of a intializer the default constructor must be called, but Character has none. Commented May 15, 2023 at 9:23
  • 1
    : val(val) is a member initializer list. As you can see here godbolt.org/z/bY74Y1Pos the error goes away when you use it Commented May 15, 2023 at 9:30
  • 1
    no its not the same. Members are initialized before the body of the constructor is executed. One way to provide an initializer is via the member initializer list. en.cppreference.com/w/cpp/language/constructor Commented May 15, 2023 at 9:37
  • 1
    no worries. I think its the one detail with the worst "beginner awareness" vs importance quotient ;). I suppose one has to get stuck at least once to understand it. Btw I already wasted my vote, but perhaps others will be so kind to find duplicate questions Commented May 15, 2023 at 9:40

1 Answer 1

1

Credit to 463035818_is_not_a_number for pointing this out!

This below:

Node (const Node<T> &other) {
    val= other.val;
}

Should be replaced with this.

Node (const Node &other) : val(other.val) {        }

I misunderstood the usage and property of member initialization list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.