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:
#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!
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.