I have a problem. I need to pass a non-static data member in one class (class Disks, data member disks) to another class (ToH) to its static bool function, so I can test that value.
Here is my error:
: error C2597: illegal reference to non-static member 'Disks::disks'

My code:

#include "Disks.h"			// include definition of class Disks

//constructor to initialize the data member 
Disks::Disks(int enteredInteger)
{
	setVariables(enteredInteger);
}
// set function to set the values for the data members
void Disks::setVariables(int enteredInteger)
{
	disks = enteredInteger;		// 3
	choice = enteredInteger;	// peg 3
}

void Disks::towers( int disks,  int start,  int end,  int temp )
{
	if ( disks == 1 )			
	{
		cout << start << " --> " << end << endl;
	}
	else	// recursion step
	{
		towers( disks-1, start, temp, end );
		cout << start << " --> " << end << endl;
		towers( disks-1, temp, end, start);
	}
}


void Disks::transfer()
{
	cout << "Instructions to move disks:";
	cout << endl
		<< "1)     1 --> 2" << endl
		<< "2)     1 --> 3" << endl
		<< "3)     2 --> 1" << endl
		<< "4)     2 --> 3" << endl
		<< "5)     3 --> 1" << endl
		<< "6)     3 --> 2" << endl;

	cout << endl;

	switch(choice)
	{
		case 1:
			towers( disks, 1, 2, 3 );
			break;
		case 2:
			towers( disks, 1, 3, 2 );
			break;
		case 3:
			towers( disks, 2, 1, 3 );
			break;
		case 4:
			towers( disks, 2, 3, 1 );
			break;
		case 5:
			towers( disks, 3, 1, 2 );
			break;
		case 6:
			towers( disks, 3, 2, 1 );
			break;
	}
}

// test function in class ToH uses a test method to test the recursion
bool ToH::test()
{
	bool answer;


	if(Disks::disks == 1)  // this is my problem / error
	{
		answer = true;
		cout << "Function passed.";
	}
	else
	{
		answer = false;
		cout << "Function failed.";
	}

	return answer;
}

Recommended Answers

All 2 Replies

Member Avatar for jencas
if(Disks::disks == 1)  // this is my problem / error

is only valid, if disks is static. But

void Disks::setVariables(int enteredInteger)
{
	disks = enteredInteger;		// 3
	choice = enteredInteger;	// peg 3
}

makes me think, that disks is a member variable of class Disks.
If so, consult a OOP book to read up on the difference between class and instance.

I need to pass a non-static data member in one class (class Disks, data member disks) to another class (ToH) to its static bool function, so I can test that value.

In that case, modify the ToH::test() to take as an argument a variable of type Disks , i.e.

bool ToH::test(Disks & obj)
// or maybe ...
// bool ToH::test([B]const [/B]Disks & obj)
{
    bool answer;

    // if disks is a private/protected member, you might change it to public
    // or make ToH friend of Disks or add a method e.g. GetDisks().
    if(obj.disks == 1)
    ...
}
// and used like
int main()
{
    Disks obj(3);
    bool b = ToH::test(obj);
    ...
}

or pass just the value of disks, so it would be

bool ToH::test(const int disks)
{
    bool answer;

    if(disks == 1)
    ...
}
// and used like
int main()
{
    Disks obj(3);
    bool b = ToH::test(obj.disks);
    ...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.