Forward Declaring Private Methods

I'm new to OOP and trying to create a singleton that requires extensive initialization. I am declaring the private constructor in the header file and trying to define that constructor in a separate .cpp file.

Dev-Cpp (using gcc) complains that the private attributes I'm trying to initialize and the constructor are private, i.e., not visible.

Does C++ require private functions to be defined when they're declared, or is there something I'm overlooking in using private methods? I'm guessing that my mistake is transparently obvious, but I've included the files and error log after my sig if you need to see how I'm screwing up.

Lance ==)------------

//random.h

#ifndef PRNG_CLASS_H
#define PRNG_CLASS_H
#include <stddef.h>

class PRNG_Class
{
private:
int value_m;
static PRNG_Class * only_instance_s;
PRNG_Class(); // hide default constructor
~PRNG_Class(); // destructor
public:
int get_value();
void set_value( int v );
static PRNG_Class * instance();
}; // class PRNG_Class
#endif
***************************************
//random.cpp

#include "random.h"
#include <stdlib.h> // for rand/srand
#include <time.h> // for time()

using namespace std;
/*
PRNG_Class::PRNG_Class( int v )
{
only_instance_s->value_m = v;
}; // constructor
*/
int PRNG_Class::get_value()
{
return PRNG_Class::only_instance_s->value_m;
}; // get_value

void PRNG_Class::set_value( int v )
{
PRNG_Class::only_instance_s->value_m = v;
}; // set_value

static PRNG_Class::PRNG_Class * instance()
{
if (!PRNG_Class::only_instance_s)
{
PRNG_Class::only_instance_s = new PRNG_Class;
PRNG_Class::only_instance_s->value_m = 0;
};
return PRNG_Class::only_instance_s;
}; // instance
***************************************
//random.log

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Dev-Cpp\Random\random.cpp" -o "C:\Dev-Cpp\Random\random.exe"
C:\Dev-Cpp\Random\/random.h: In function `PRNG_Class* instance()':
C:\Dev-Cpp\Random\/random.h:19: error: `PRNG_Class*PRNG_Class::only_instance_s' is private C:\Dev-Cpp\Random\random.cpp:28: error: within this context
C:\Dev-Cpp\Random\/random.h:19: error: `PRNG_Class*PRNG_Class::only_instance_s' is private
C:\Dev-Cpp\Random\random.cpp:30: error: within this context
C:\Dev-Cpp\Random\/random.h:20: error: `PRNG_Class::PRNG_Class()' is private
C:\Dev-Cpp\Random\random.cpp:30: error: within this context
C:\Dev-Cpp\Random\/random.h:19: error: `PRNG_Class*PRNG_Class::only_instance_s' is private
C:\Dev-Cpp\Random\random.cpp:31: error: within this context
C:\Dev-Cpp\Random\/random.h:18: error: `int PRNG_Class::value_m' is private
C:\Dev-Cpp\Random\random.cpp:31: error: within this context
C:\Dev-Cpp\Random\/random.h:19: error: `PRNG_Class*PRNG_Class::only_instance_s' is private C:\Dev-Cpp\Random\random.cpp:33: error: within this context Execution terminated

I have noted a few errors in your .cpp file. You declared the function name incorrectly in the definition

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
int PRNG_Class::get_value()
{
	return PRNG_Class::only_instance_s->value_m;
}


void PRNG_Class::set_value(int v)
{
	PRNG_Class::only_instance_s->value_m = v;
}


// don't use static here
// Qualify the function name with the class name
PRNG_Class* PRNG_Class::instance()
{
	if(!PRNG_Class::only_instance_s)
	{
		PRNG_Class::only_instance_s = new PRNG_Class;
		PRNG_Class::only_instance_s->value_m = 0;
	};
	return PRNG_Class::only_instance_s;
}

// Actually define the static variables
PRNG_Class * PRNG_Class::only_instance_s;
Also don't use the old headers <stdlib.h> with C++ you should use <cstdlib> instead:

1
2
3
#include <cstdlib> // don't use old .h version
#include <ctime> // ditto
#include <cstddef> // ditto 
Thanks for spotting the incorrect qualification; I imagine that when I get home, it'll make a world of difference.

It makes sense to put the static variables in the .cpp file rather than the .h file. Why do so many examples put them in the private section of the header?

Also, thanks for the tip on using new-style headers vs. the old c-style ones.

Lance ==)------------------
Topic archived. No new replies allowed.