Please help debug this friend class in C++


 
Thread Tools Search this Thread
Top Forums Programming Please help debug this friend class in C++
# 1  
Old 12-15-2010
Please help debug this friend class in C++

Please help check an old code:

#include <iostream>
using namespace std;

template <class C>
class List {
//Linked list of C.
template<class U>
friend class ListItr;
private:
class ListEl {
public:
C val;
ListEl* next;
ListEl(const C& s, ListEl* n) : val(s) { next = n;}
};
public:
List() { head = tail = NULL; size = 0;}
~List() {
if(head) {
ListEl *p=head;
for(ListEl *q=p->next;q;p=q,q=p->next)
delete p;
delete p;
}
}
void add(const C& s) {
size++;
if(head)
tail = tail->next = new ListEl(s,NULL);
else
head = tail = new ListEl(s,NULL);
}

int getSize() const { return size; }
private:
ListEl *head, *tail;
int size;
};

template <class U>
class ListItr {
// List iterator.
private:
List::ListEl<U> * current; // Here: why not work ??
public:
ListItr(List<U>& list) { current = list.head; }
int more() { return current!=NULL; }
U& next() { U& v = current->val; current = current->next; return v;
}
};

// Simple example uses type int
main()
{
List<int> L;
L.add(0); // Insert a new element
L.add(0); // Insert a new element
L.add(5);
L.add(6);
cout << endl;
return 0;
}


Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

Thank You.

The UNIX and Linux Forums.

Last edited by Scott; 12-16-2010 at 03:51 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies

2. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

3. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies

4. Programming

static use for class inside the same class c++

Hi, I believe the next code is wrong: class Egg { Egg e; int i; Egg(int ii=0) : i(ii) {} }; because you would end up with an endless definition (memory allocation) of Egg objects, thus int i. Ok, so God Eckel proposes for a singleton: class Egg { static Egg e; int... (5 Replies)
Discussion started by: xavipoes
5 Replies

5. Programming

C++ class definition with a member of the same class

Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class? For example, a linked list or i want to convert this C code to C++ class (Elemento) typedef struct elemento { char name; char value; List<struct elemento> ltElementos; ... (7 Replies)
Discussion started by: pogdorica
7 Replies

6. UNIX for Dummies Questions & Answers

car class (not school class)

im just trying to have some fun and kill some time writing a c++ program that has a person type in a car make and model then gives them a year and a price. or something like that. i always have problems getting it goin but once the ball is rolling im usually pretty good. anyone wanna help me out? ... (1 Reply)
Discussion started by: rickym2626
1 Replies

7. Programming

How to make a function friend to both base and derived class

Hi, I have a base class and derived a class from the base class, i want to print & read the data for the object created for the derived class,so i have overloaded both the << and >> operators and also have done the foward declaration. Below is the code snippet, #include <iostream> class... (3 Replies)
Discussion started by: ennstate
3 Replies
Login or Register to Ask a Question