problem involving constructors

Hi, so i tried running my code and i keep getting the error "call to implicitly-deleted default constructor of 'node'. My code is as follows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//.h file 
typedef struct node * point
struct node{
string data;
node *next
}

class list{
somefunction(int);

//.cpp file
somefunction(int)
{
point ptr1, ptr2;
ptr1 = new node; 

I get the error at the line "ptr1 = new node;" I tried putting a default constructor for my node struct and that fixed the problem but a new problem arises. It states that i have a linker error after i compile it with a default constructor.
It does not looks like default constructor should be deleted in code your posted, unless node is more complex class.

If it is deleted, you should provide it. Your linker error is probably because you declared, but not defined it.
Try following:
1
2
3
4
struct node
{
    node() = default;
//... 
Thank you for your reply. However when i did that it states "Constructor for 'node' must explicitly initialize the member 'data' which does not have a default constructor"
this should not happen unless data type is not an std::string but some other custom string class. Is this the case?
yes, it is a custom class. I just put string because I didn't think it had anything to do with the problem. It was a custom class called "employee". I put a default constructor employee::employee{}; and my program ran normally. There was no more errors. Why is this?
Because when object is constructed, it should initialize its members. If you do not specify otherwise, it does it by calling their default constructors. If those are nonexistant or inaccessible, compilation fails.
I see. Thank you so much for your time and reply. I really appreciate it
> I just put string because I didn't think it had anything to do with the problem
It is good to simplify the code, just make sure that the code does reproduce the problem.


If you want to use another constructor you can call it using an initialization list
1
2
3
4
5
6
struct node{
   std::string data;
   node *next;
   node(): data("NULL"), next(NULL) {}
   node(const std::string &data): data(data), next(NULL){}
};
Topic archived. No new replies allowed.