Declaration problems

I am trying to make a function that recursively adds up the data areas of a linked list and finds the sum of all the cells added together. I put the class together but it does not like how I am declaring my list in the main.

Anyone have any idea whats wrong with it?
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iomanip>
#include <iostream>

using namespace std;

struct node
{
int data;
node* next;
};

class Link
{
private:
/* struct node
{
int data;
node* next;
};
*/
node *head;
public:
Link();
int sum(node *head);
void push(int item);
};

int main(){


// Find the sum of the inputted numbers
int Link z;

z.push(1);
z.push(4);
z.push(2);
z.push(3);
cout << "Should equal 10" << endl;
cout << z.sum() << endl;

return 0;
}

Link :: Link()
{


}

void Link :: push(int item)
{
node *t = new node;
t -> data = item;
t -> next = head;
head = t;
}

int Link :: sum (node *head)
{
if(head == NULL)
return 0;
else
return head ->data + sum(head ->next);
}

Last edited on
Just use List z;. There is no reason to put int before it.

It seems like you are trying for a template of some kind, but if you just want to fix the problem remove int from in front of your declaration.
closed account (D80DSL3A)
Line 32 should be Link z;.
Your sum() takes a node* as a parameter so line 39 cout << z.sum() << endl; won't work.

I modified your code a bit and got it to work. Unfortunately head is no longer private here since it must be supplied as an argument to sum(). Can you think of a better way to make this work (so that head can remain private)?

I think your constructor ought to initialize head = NULL since the list is empty.

I also changed the node* used in sum() to pNode from head because head is just the starting value. You don't want to be changing the value of head in the sum().

Nice job on the recursive function! It works fine. I get 10 for the sum.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Link
{	
public:
	node *head;
	Link();
	int sum(node *pNode);
	void push(int item);
};

Link :: Link()
{
	head = NULL;
}

void Link :: push(int item)
{
node *t = new node;
t->data = item;
t->data = item;
t->next = head;
head = t;
return;
}

int Link :: sum (node *pNode)
{
if(pNode == NULL)
return 0;
else
return pNode->data + sum(pNode->next);
}

int main()
{
// Find the sum of the inputted numbers
Link z;

z.push(1);
z.push(4);
z.push(2);
z.push(3);
cout << "Should equal 10" << endl;
cout << z.sum(z.head) << endl;

return 0;
}
thank you for your help guys ima take what you all did into consideration and even use what u said fun2code thanks allot for taking the time to run it your self im gana fix it right now :)
awesome it works now, just had to re add all of my stuff i did before and no problem at all thank you again
Topic archived. No new replies allowed.