Access structure in a structure with template

Hi guys , This is one of my school work that requires me to read a customer.txt and stores the information inside into a list .

I'm doing the readfile function part now , and my idea is read the file -> capture a node -> insert it into a list

and then I encounter this problem

This is node.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef Node_type
#define Node_type

template <class T>
class Node {
	public:
		T		item;
		Node	*next;
		Node(T);
};

template <class T>
Node<T>::Node(T newItem) {
	item = newItem;
	next = NULL;
}

#endif 


this is readfile.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef readfile
#define readfile


using std::string ;

class customer
{
	string account,name,address,dob,phone;
	double balance;

	
}
void readFile();

#endif 


this is the readfile.cpp

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
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include "readfile.h"
#include "Node.h"
#include "List.h"

using namespace std ; 

void readFile()

{
	string file;
	string line;

	file=name;//ignore this 
	cout<<endl;

	ifstream infile(file,ifstream::in) ;
	
	if(!infile.good())
	 {
		 cout<<"File 1 open error";
		 cout<<"\nPlease press any button to continue";
		 cin.get();
		 system("cls");
	 }
	
	else 
	{		
		 while (!infile.eof())
		 {
			 Node<customer>::item //this part 


			 
		 }
		


	infile.close();

	}

}


What should I do to let me access to the items inside the class customer?

Appreciate for any help

It has this error too .


2 IntelliSense: a nonstatic member reference must be relative to a specific object
Last edited on
closed account (zb0S216C)
The line you pointed out: You're treating Node::item, a non-static data member, as a static member - only static data members can be qualified with the class identifier. Since Node::item is non-static, the compiler requires an object of Node (instance) to access Node::item. For example:

1
2
Node<int> New_Node_;
New_Node_.item = ...;

Edit: What were you trying to do on that line anyway? Just curious.

Wazzak
Last edited on
I want to access to the class and store the data written in the file customer.txt

each customer in the txt has these information

account,name,address,dob,phone,balance , I would read these into item and then insert them into a list .
Before you get to the Nodes, you need to build a customer object:

1
2
3
customer newCust;
newCust.name =...
// etc 


Then you need to load it into what appears to be a singly linked list. Note that you'll need to somehow keep track of these nodes outside that while() block.
I tried to write this code and it doesn't work

1
2
3
4
5
6
while (!infile.eof())
		 {
			Node<customer> newnode;

			 
		 }


According to my understanding (which is very little) In here I'm building a Node object named newnode and setting the templates to be customer , that means the content of Node class now should be

1
2
3
4
5
6
7
template <class T>
class Node {
	public:
		customer		item;
		Node	*next;
		Node(customer);
};


Am I right ?

But it has this error


7 IntelliSense: no default constructor exists for class "Node<customer>"

(Actually I have a ton more error but this is the latest I found after I changed the code )
You have no default constructor for your Node class since you defined the non-default constructor:

1
2
3
4
5
template <class T>
Node<T>::Node(T newItem) {
	item = newItem;
	next = NULL;
}


So, to instantiate a Node object, you must use this non-default constructor. This constructor expects an argument of type T (in this case, your customer class). Thus, when you create your object, you need to use something like this:

Node<customer> newnode(customerObject);

where customerObject is an already instantiated object of the customer class
After some work the code manages to remove most of the error so I move on to the int main() , then this appear


Error 1 error C3861: 'readFile': identifier not found

2 IntelliSense: identifier "readFile" is undefined

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include "List.h"
#include "Node.h"
#include "readfile.h"

using namespace std ; 

customer readFile(string name)

{
	string file;
	string line;
	int number;
	customer input;
	List<customer> list ;

	file=name;
	cout<<endl;

	ifstream infile(file,ifstream::in) ;
	
	if(!infile.good())
	 {
		 cout<<"File 1 open error";
		 cout<<"\nPlease press any button to continue";
		 cin.get();
		 system("cls");
	 }
	
	else 
	{		
		 while (!infile.eof())
		 {
			 Node<customer> newnode(input);

			 cin.ignore(256,'=');//ignore until '='
			 
			 cin>>number;//get account number
			 
			 newnode.item.account = number ;

			 cin.ignore(6,' ');

			 newnode.item.name = cin.get();

			 cin.ignore(9,' ');

			 newnode.item.address=cin.get();

			 cin.ignore(4,' ');

			 newnode.item.dob=cin.get();

			 cin.ignore(14,' ');

			 newnode.item.phone = cin.get();

			 cin.ignore(17,' ');

			 newnode.item.balance = cin.get();
			 
			 list.insert(input);
		 }
		


	infile.close();

	}

}

bool customer::operator>= (customer c2) {
	if (name.compare(c2.name) >= 0)
		return true;
	return false;
}

bool customer::operator== (customer c2) {
	if (name.compare(c2.name) == 0)
		return true;
	return false;
}


new code of readfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "readfile.h"
#include "Node.h"
#include "List.h"

using namespace std ;

int main()
{
	string name;
	
	cout<<"Please enter name of file:";
	cin>>name;

	readFile(name);

}

main file , I checked the spelling and stuff , it doesn't seems like it's the spelling problem , do I need to declare a prototype in the main file if I'm calling function from other sources ? Aren't that handled by the header file ?
Please post full files readfile.h, readfile.cpp (assuming it exists), and your file containing int main().
readfile.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef readfile
#define readfile


using std::string ;

class customer
{
public:
	string name,address,dob,phone;
	double balance;
	int account;
	bool operator>=(customer);
	bool operator==(customer);
	customer readFile(string);
};

#endif 


readfile.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include "List.h"
#include "Node.h"
#include "readfile.h"

using namespace std ; 

customer readFile(string name)

{
	string file;
	string line;
	int number;
	customer input;
	List<customer> list ;

	file=name;
	cout<<endl;

	ifstream infile(file,ifstream::in) ;
	
	if(!infile.good())
	 {
		 cout<<"File 1 open error";
		 cout<<"\nPlease press any button to continue";
		 cin.get();
		 system("cls");
	 }
	
	else 
	{		
		 while (!infile.eof())
		 {
			 Node<customer> newnode(input);

			 cin.ignore(256,'=');//ignore until '='
			 
			 cin>>number;//get account number
			 
			 newnode.item.account = number ;

			 cin.ignore(6,' ');

			 newnode.item.name = cin.get();

			 cin.ignore(9,' ');

			 newnode.item.address=cin.get();

			 cin.ignore(4,' ');

			 newnode.item.dob=cin.get();

			 cin.ignore(14,' ');

			 newnode.item.phone = cin.get();

			 cin.ignore(17,' ');

			 newnode.item.balance = cin.get();
			 
			 list.insert(input);
		 }
		


	infile.close();

	}

}

bool customer::operator>= (customer c2) {
	if (name.compare(c2.name) >= 0)
		return true;
	return false;
}

bool customer::operator== (customer c2) {
	if (name.compare(c2.name) == 0)
		return true;
	return false;
}


main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "readfile.h"
#include "Node.h"
#include "List.h"

using namespace std ;

int main()
{
	string name;
	
	cout<<"Please enter name of file:";
	cin>>name;

	readFile(name);

}
Okay, in readfile.h, you define class 'customer' which has a member function 'readFile(...)'. This means that only objects of type 'customer' can have readFile called on them, like:

1
2
customer MyFavoriteCustomer;
MyFavoriteCustomer.readfile(...);


in main, you are calling 'readFile(name)', which can't work because there is no customer object! Also, in readfile.cpp, you are trying to give an implementation for readFile(), but that is not the right way to do it. You need to qualify the function as being part of 'customer', so the definition needs to look something like:

1
2
 
customer customer::readFile(string name)


There are other problems...readFile is supposed to return a customer, but doesn't. I suspect readFile is NOT intended to be a member function of customer. There's no reason to copy string 'name' into a new string 'file'. You never do anything with the list of customers created in readFile.

You may be better off taking a step back and rethinking your logic.
Last edited on
Okay somehow I got the readfile function working , then I proceed into working the other header display function and I encounter this problem that I have no idea why it even happen

display.h
1
2
3
4
5
6
#ifndef display
#define display

void display();

#endif 


1
2
3
4
5
6
7
8
#include <iostream>
#include "display.h"

display.cpp
void display()//error here 
{                   //and here
	cout<<"show stuff ";	
}


It says
5 IntelliSense: expected an identifier

6 IntelliSense: expected a ';'

What now ...
#define display here you define display to be nothing. All occurrences of display will be replaced by nothing.
1
2
3
4
void display()
{
	cout<<"show stuff ";	
}
becomes
1
2
3
4
void ()
{
	cout<<"show stuff ";	
}


Use unique header guards (like DISPLAY_H) that doesn't clash with anything else in the programs.
Hi guys , I'm now working on the delete record function ,

I'm facing problem again (I know , I'm stupid )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "List.h"
#include "readfile.h"
#include "deleteRecord.h"
#include <iostream>


void deleteRecord(List<customer> *list,customer &input)

{
	int record;
	std::cout<<"Enter ID to be removed: "<<std::endl;
	std::cin>>record;

	//Node<customer> newnode(input);

	list->find(input)->item.account==record;

        list->remove();
}


What I understand is the list->find(input)->item.account==record;

will find the node that contains the value that is stored in record and return the node , the problem is next step , even I got the node , how do I find and put the position of the node into the remove function to let it know that I need this node to be removed ?

Greatly appreciate for any help !
Last edited on
Topic archived. No new replies allowed.