Non-static member functions

Feb 16, 2010 at 3:40am
Inheritance is not as cooperative as I want it to be. All my base classes are fully functioning, tested, but my entryType class will not let me call the print function of any of the other classes. Error:
'personType::printName' : illegal call of non-static member function


This is the same error that originates three times from the line entry[0].printEntry();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "entryType.h"
#include <string>

using namespace std;

int main()
{
	entryType entry[500];
	entry[0].setAddress("Las Vegas Blvd", "Las Vegas", "NV", 92017);
	entry[0].setDate(7,4,1980);
	entry[0].setName("Harrah's", "Casino");
	entry[0].printEntry();
	return 0;
}


I know it's members are not really inherited, but can someone please tell me what I'm doing wrong?

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
#ifndef H_entryType
#define H_entryType
#include "addressType.h"
#include "personType.h"
#include "dateType.h"
#include <iostream>
#include <string>

using namespace std;

class entryType
{
public:
	void setName(string,string);
	void setDate(int,int,int);
	void setAddress(string,string,string,int);
	void printEntry();
	entryType();
private:
	addressType address;
	dateType birthday;
	personType name;
};

entryType::entryType()
{
	personType::personType();
	dateType::dateType();
	addressType::addressType();
}

void entryType::printEntry() 
{
	personType::printName();
	dateType::printDate();
	addressType::printAddress();
}
#endif 
Feb 16, 2010 at 3:48am
This isn't inheritance. This is composition. IE, entryType isn't a child class or anything (that would be inheritance). Instead it just "owns" a few objects of other types.

You call member functions in the form object.function();. Here, address is your object.. so it would be address.printAddress(); (not addresType::printAddress)

Also...

1
2
3
4
5
6
entryType::entryType()
{
/*	personType::personType();  // get rid of this crap
	dateType::dateType();
	addressType::addressType();*/
}
Last edited on Feb 16, 2010 at 3:49am
Feb 16, 2010 at 5:08am
lol I was trying to make the member functions static and just about everything else I wasn't supposed to do. Do you know how long I tried to figure this out? Longer than I want to say.
Thanks for the info Disch.

So those constructors aren't needed?
Feb 16, 2010 at 5:31am
Not only are they not needed, but they're not doing what you think. They're creating a temporary object (and then destroying it right away). Utterly pointless.

Default constructors (those which take no parameters) are called by default (hence the name). You don't need to worry about explicitly constructing anything unless you want to use a specific ctor... one other than the default.


To elaborate a bit more on what your original problem was (even though it's solved now):

Remember that to call a (nonstatic) member function you need an object. You always have to tell the compiler what object you want to perform the function on.

For instance:

1
2
3
void entryType::printEntry() 
{
	personType::printName();  // whose name are you trying to print?  you never say! 


It may seem obvious to you because you only have one personType in your class, the compiler can't make that assumption. What if you have multiple personTypes?
Topic archived. No new replies allowed.