Cannot create struct that contains instance of a class

Hello programmers,

I'm trying to learn as much C++ as I can. I was writing a program that mixes linked lists and classes. There is the class "Obj" which only holds an integer called 'data' and the classic "struct node" structure for linked list, but this time the "node" structure will hold an instance of "Obj" Class and the next* pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include <iostream>

using namespace std;

class Obj
{
private:
	int data;
public:
	Obj(int d): data(d) {}
	void getData() {cout<<data<<endl;}
};

struct node
{
	Obj o;
	node* next;
};

int main()
{
	node* n=new node; //I receive "Call to implicity-deleted default constructor of 'node'" here
}


Unfortunately I'm not able to create a new node because of the error I mention. Do you know what is the problem here? Perhaps is something really stupid to ask this but I literally hit my head against the desk trying to find out the solution.

Cheers!
Obj requires a default constructor to be used in the way you are using it. The constructor you've given Obj requires in integer value.
@kbw: You Sir, are totally right! Thanks for that quick and accurate response.

Now the code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

class Obj
{
private:
	int data;
public:
	Obj() {data=0;}
	void getData() {cout<<data<<endl;}
	void setData(int value) {data=value;}
};

struct node
{
	Obj o;
	node* next;
};

int main()
{
	node* n=new node;
	n->o.setData(15);
	n->next=NULL;
	
	n->o.getData();
}


Console outputs: 15

Cheers!
Topic archived. No new replies allowed.