Calling a method of one class in the method of another.

Feb 14, 2013 at 3:31am
I'm trying to write a program, which is split into two parts, and before to finish the first part I need write two classes, each consisting of a .h file and a .cpp file. This is my first time writing a program that involves multiple files. Anywho, the problem is that in my class Seller, I have a method called print, and in my class SalesDB, I have have to write a method print which involves calling Seller::print multiple times.

My program compiles fine, but doesn't build, I have the error message below.
I think the problem is my syntax, and I haven't found any solutions searching the web for the past couple days, most times it just resulted in creating more errors. Also, this is the syntax I've been told needs to be used:

arrayName[subscript].methodName(arguments); (for the line inside the for loop inside the last method of the salesDB class)

Here is my code:

Seller.h : the build error also points to this error
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
#ifndef SELLER_H
#define SELLER_H

class Seller
	{

	public:
	
		//Data members
	
		char name[31];
	
		double salesTotal;
	
		
		//Method prototypes
		
		Seller();
	
		Seller(char*, double);
	
		char* getName();
	
		double getSalesTotal();
	
		void setSalesTotal(double);
	
		void print();
	
	};

#endif /* SELLER_H*/ 


Seller.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
#include "Seller.h"
#include <iostream>
#include <iomanip>

using namespace std;


/***************************************************************
   FUNCTION: Seller constructor
***************************************************************/ 
Seller::Seller()
	{
	
	name[0] = '\0';
	salesTotal = 0;	   
	
	}


/***************************************************************
   FUNCTION: alternate Seller constructor
***************************************************************/ 
Seller::Seller(char* newName, double newSalesTotal)
	{
	
	strcpy( name , newName ); 
	salesTotal = newSalesTotal;
	
	}



/***************************************************************
   FUNCTION: getName
***************************************************************/ 

char* Seller::getName()
	{
	
	return name;
	
	}



/***************************************************************
   FUNCTION: getSalesTotal
***************************************************************/ 

double Seller::getSalesTotal()
	{
	
	return salesTotal;
	
	}



/***************************************************************
   FUNCTION: setSalesTotal
***************************************************************/ 

void Seller::setSalesTotal(double newSalesTotal)
	{
	
	salesTotal = newSalesTotal;
	
	}

/***************************************************************
   FUNCTION: print
***************************************************************/ 

void Seller::print()
	{
	
	cout<<left<<setw(30)<<name
		<<right<<setw(9)<<fixed<<setprecision(2)<<salesTotal;
	
	}



SalesDB.h
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
#ifndef SALESDB_H
#include "Seller.h"
#define SALESDB_H

using namespace std;

class SalesDB
        {

	public:
	
		//Data members
			
		char dataBase[31];
	
		int sellersTotal;
	
	
		//Method prototypes
		
		SalesDB();
	
		SalesDB(const char*);
	
		void print();
		
	};

#endif /* SELLER_H*/



SalesDB.cpp : this one contains the error in the for loop in the method print
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
#include "SalesDB.h"
#include "Seller.h"
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;


/***************************************************************
   FUNCTION: SalesDB constructor
***************************************************************/ 
SalesDB::SalesDB()
	{
	
	sellersTotal = 0;	 
	
	}




/***************************************************************
   FUNCTION: SalesDB alt constructor
***************************************************************/ 

SalesDB::SalesDB(const char* pointer)
	{
	
	ifstream inFile;
	
	inFile.open("sales.txt"); 
	
	if ( inFile.fail() ) 
		{
		cout << "open for test.dat failed";

		exit(1);
 		}
	
	inFile.read((char*) this, sizeof(SalesDB));
	
	inFile.close();
	
	}



/***************************************************************
   FUNCTION: print
***************************************************************/ 

void SalesDB::print()
	{
	
	cout<<left<<setw(30)<<"Sales Database Listing"
		<<right<<setw(9)<<"Sales";
		
	for ( int i = 0 ; i <= sellersTotal ; i ++)
		{
		
		cout<<Seller::name[i].print;
		
		}
		
	
	}



this is my first time posting here, so I don't know how to copy and paste my build error, so I'm gonna type most of it.

build error from seller.cpp
1
2
3
4
5
6
Checking dependencies
"C:\ProgramFiles(x86... some stuff about pedantic errors Wno-long-long, then lists the addresses of my object files or something
files/files/Seller.h: In member function 'void SalesDB::print()':
files/files/Seller.h: 21: error: object missing in reference to 'Seller::name;
files/files/\salesdb.cpp:74 error: from this location
Unsuccessful build 


build error from salesDB.cpp
1
2
3
4
5
6
Checking dependencies
"C:\ProgramFiles(x86... some stuff about pedantic errors Wno-long-long, then lists the addresses of my object files or something
Seller.h: In member function 'void SalesDB::print()':
Seller.h: 21: error: object missing in reference to 'Seller::name;
salesdb.cpp:74 error: from this location
unsuccessful build 


Let me know if you need anymore details! Thanks!
Feb 14, 2013 at 3:39am
The only error I can see is line 62 in SalesDB.cpp: You are trying to access Seller::name, which doesn't make any sense in this function.

I'm not quite sure what you were trying to do there.
Feb 14, 2013 at 3:47am
Yeah that's where my problem is, as per my directions for this project, I'm supposed to call the print method of the seller class in that line
Feb 14, 2013 at 5:51am
It sounds to me that the salesDB class should be composed of seller objects - that is salesDB should have an array or std::list of seller objects.

The salesDB print function could iterate or loop through the list or array and call the seller obj print function.

HTH
Feb 19, 2013 at 5:45am
Ok sounds good, how exactly do include an array of seller objects, do i actually use "#include"? I don't think I know the correct syntax or where to place it.
Feb 19, 2013 at 7:37am
In SalesDB.cpp , Declare an array which is of type Seller class say ,

Seller OBJ[MAX];

Then call these array of objects inside the loop like specified below .

cout << OBJ[i].print();


Topic archived. No new replies allowed.