Hello, I am newbie of C++
Below is my code, I want to implement the function "printall " to print out the " contact" data in Dlinked_list. but I faced a problem to call out the function of class "Contact" of "print".
for example:
list.insert(new Contact("Ada", "ada@ust.hk", "12345678"));
then the "Contact" data in inside the class.
But how can I print out the data of "contact"?
Thank you!

#include <iostream>
#include "contact.h"
#include "dlinked_list.h"

int main() 
{	
	printf("#####Test begins:#####\n");
	Dlinked_list list;
	printf("Insert %u contacts.\n", 2);
	list.insert(new Contact("Ada", "ada@ust.hk", "12345678"));

	list.printAll();
	return 0;
}
#ifndef _dlinked_list_
#define _dlinked_list_

#include "contact.h"

/* The definition of the struct of doubly-linked list node.
	struct is used for simplicity.
*/
typedef struct DLL_node {

	DLL_node *prev;
	DLL_node *next;
	Contact *contact;

} DLL_node;
// end of DLL_node struct definition


/* The definition of the class of doubly-linked list.
	Functions are defined as prototypes here.
*/
class Dlinked_list {

public:

	// Default constructor
	Dlinked_list();

	// Destructor
	~Dlinked_list();

	// Append a node in the end of the linked list
	int insert(Contact *c);

	// Append a node at the indexed position of the linked list
/*	int insertAt(Contact *c, int index);

	// Remove the first node of the linked list
	int remove();

	// Delete the node at the indexed position of the linked list
	int removeAt(int index);

	// Return the index of the first occurrence of the matching node
	int indexOf(const char* name);

	// Return the minimum index of the nodes with a lexigraphically larger "name"
	int indexOfMin(const char * name);
*/
	// Display contents of all the nodes
	void printAll();
/*
	// Display content of the indexed node
	void printAt(int index);

	// Display contents of all the nodes in reverse order
	void printReverse();

	// Remove all the nodes from the linked list
	void clear();

	// Return number of nodes in the linked list
	int length();

	// Indicates whether the linked list has no nodes or not
	bool isEmpty();
	*/

private:

	// Pointer to the head of the linked list
	DLL_node *head;

	// Pointer to the tail of the linked list
	DLL_node *tail;

	// Size of the linked list
	int size;


}; // end of Dlinked_list class definition

#endif
#ifndef _contact_
#define _contact_

#include <iostream>
using namespace std;

#include <string.h>



class Contact {

public:

	// Default constructor
	Contact() {
		strcpy(name, "no name");
		strcpy(email, "nosuchperson@ust.hk");
		strcpy(phone, "12345678");
	};

	// Parametric constructor
	Contact(char* n, char *e, char *p) {
		strcpy(name, n);
		strcpy(email, e);
		strcpy(phone, p);
	};

	// Destructor
	~Contact() { /* Do nothing */ };

	// Getter
	char* getName() { return name; }
	// Getter
	char* getEmail() { return email; }
	// Getter
	char* getPhone() { return phone; }

	// Setter
	void setName(const char* n) { strcpy(name, n); }
	// Setter
	void setEmail(const char* e) { strcpy(email, e); }
	// Setter
	void setPhone(const char* p) { strcpy(phone, p); }

	// Display content
	void print() {
		cout << " Name: " << name << endl;
		cout << "email: " << email << endl;
		cout << "Phone: " << phone << endl;
	}

private:

	char name[30];
	char email[30];
	char phone[10];

}; // end of Contact class definition

#endif

Let pnode is a pointer to a DLL_node.
Now

pnode->contact->print();

prints this node.
Can you write the code traversed linked lists from the head to the tail?
Is it your "problem behind problem"?

Let pnode is a pointer to a DLL_node.
Now

pnode->contact->print();

prints this node.
Can you write the code traversed linked lists from the head to the tail?
Is it your "problem behind problem"?

My code works right now! Thank you for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.