Hello,

I faced a problem to use a function in struct format. If I insert the data to the Contact("Ada", "ada@ust.hk", "12345678")). How can I print it out by using the Contact class member "print"? Thank you.

typedef struct DLL_node {

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


} DLL_node;


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

Well, and what's your problem?

Contact todo("Please","look@your.textbook","12345");
todo.print();

Is it a problem?

Well, and what's your problem?

Contact todo("Please","look@your.textbook","12345");
todo.print();

Is it a problem?

Thank you for your reply
I forgot to say that I will use a class member function in Dlinked_list.
the contact saved in currnode->contact.
Inside the void Dlinked_list::printAll() , the (currnode->contact).print not work.
How can I fix it?

Dlinked_list list;
list.insert(new Contact("Ada", "ada@ust.hk", "12345678"));

typedef struct DLL_node {

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


} DLL_node;


void Dlinked_list::printAll()
{
DLL_node* newnode =	new DLL_node;
DLL_node* currnode = head;


(currnode->contact).print <-this code not work

}

here my full code main.cpp

#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.insert(new Contact("dfgfg", "ada@ust.hk", "453543"));
	list.insert(new Contact("dfgfg", "ada@ust.hk", "453543"));
	list.printAll();
	
	return 0;
}

contact.h

#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

dlinked_list.cpp

#include "dlinked_list.h"
#include <string.h>

Dlinked_list::Dlinked_list()
{
	head = NULL;
	tail = NULL;
	size = 0;
}

Dlinked_list::~Dlinked_list()
{
}

int Dlinked_list::insert(Contact *c)
{
DLL_node* newnode =	new DLL_node;
DLL_node* currnode = tail;
newnode->contact = c;
if (size==0)
{
newnode->next = NULL;
newnode->prev = NULL;
head = newnode;
tail= newnode;
}
else
{
newnode->next = currnode->next;
newnode->prev = currnode;
currnode->next = newnode;
tail = newnode;
}

size++;

return size;

}
void Dlinked_list::printAll()
{

DLL_node* newnode =	new DLL_node;
DLL_node* currnode = head;



while(currnode !=NULL)
{

(currnode->contact).print;  this code not work

currnode = currnode->next;
}

If print is a function... you need brackets:

(currnode->contact).print();

Thank you for your reply
I forgot to say that I will use a class member function in Dlinked_list.
the contact saved in currnode->contact.
Inside the void Dlinked_list::printAll() , the (currnode->contact).print not work.
How can I fix it?

Dlinked_list list;
list.insert(new Contact("Ada", "ada@ust.hk", "12345678"));

typedef struct DLL_node {

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


} DLL_node;


void Dlinked_list::printAll()
{
DLL_node* newnode =	new DLL_node;
DLL_node* currnode = head;


(currnode->contact).print <-this code not work

}

Hi your code is absolutely right. It is working correctly I have tested it. Well I think your calling is not correct.

currnode->contact).print;    //this is not correct
currnode->contact->print(); // this works perfect.

okay

Hi your code is absolutely right. It is working correctly I have tested it. Well I think your calling is not correct.

currnode->contact).print;    //this is not correct
currnode->contact->print(); // this works perfect.

okay

Thank you. It works now.
But I have a problem to use the function of getName ,
if show error message:

error C2660: 'getName' : function does not take 1 parameters

DLL_node* currnode = head;
	char* temp;
	currnode->contact->getName(temp);

Here is your prototype for getName:

char* getName() { return name; }

So you need :

temp = currnode->contact->getName();

I get it now. Thank you!

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.