Making a Private Out of Line Function?

Hi everyone,

I am having some issues creating a private out of line function in a class. Here is what I am starting with (and compiles fine):

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 PHONEENTRY_H
#define PHONEENTRY_H
#include "PhoneNumber.h"
using namespace std;


class PhoneEntry
{
private:
	PhoneNumber Number;
	string firstName,
			lastName;

void writeDots(ostream& fout, int n)
{	
	if (n &1)
		fout << '.';

	for (int x = 0; x<(n/2); x++)
		fout << " .";
}

public:	
		istream& readEntry(istream& fin);


		ostream& writeEntry(ostream& fout);
};


#endif 


Here is my modified code to try make it out of line, but I get a compilation error that says "One or more multiply defined symbols found":

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
#ifndef PHONEENTRY_H
#define PHONEENTRY_H
#include "PhoneNumber.h"
using namespace std;


class PhoneEntry
{
private:
	PhoneNumber Number;
	string firstName,
			lastName;

void writeDots(ostream&, int);


public:	
		istream& readEntry(istream& fin);


		ostream& writeEntry(ostream& fout);
};


void PhoneEntry::writeDots (ostream& fout, int n)
{	
	if (n &1)
		fout << '.';

	for (int x = 0; x<(n/2); x++)
		fout << " .";
}


#endif 


Any suggestions or corrections that anyone has are most appreciated. Thanks for your help in advance!


It seems ok. Does it say what the symbols are?
What are the following #include "PhoneNumber.h" used for? I think that you should remove this line.
Thank you for both for your help. The "PhoneNumber.h" line is used for the class defined in that header file. It works fine using my top code but not using the bottom one. Here is a picture of the error message:

https://dl.dropbox.com/u/66464678/Multiply%20Defined%20Symbols.png

Thanks again for your help!
You need to move the implementation of writeDots out of the header file into a .cpp file, or declare it inline.

It's being compiled in each .cpp file that the header file appears in, and the linker doesn't know which implementation to choose as it's expecting just one version of the function.
kbw,

Thanks for your help! That was exactly the problem. I moved the function into a cpp file and it compiles fine.

Thanks again!
closed account (zb0S216C)
Shocking; nobody said anything about the namespace pollution.

Wazzak
Topic archived. No new replies allowed.