Well here is my biggest jam of the day:

1) I am not sure if I am using the test method successfully in this program. I need to test
the recursion to make sure it’s working accurately. I am not sure how to test this before
it prints because I am having issues passing the data.
2) So I tested it after to at least print if the function worked successfully by making it to the
base case. However, I am having one error:

.cpp(174) : error C2597: illegal reference to non-static member 'Disks::disks'

for the line in the function definition

if(Disks::disks == 1)

My code is below.

Header file:

// Disks class definition
class Disks
{
public:
	Disks(int);			// constructor
	void setVariables(int);			
	//void validateDisks(int);		
	//void validateChoice(int);	
	
// function that contains the recursive algorithm 
	void towers(int,int,int,int);	

// function to gather the data and perform the algorithm based on the disk info
	void transfer();				

private:
	int disks;						
	int choice;						
};

class ToH
{
public:
	static bool test();				
};

Main() :

int main()
{
	// create the object
	Disks disk(3);		// number of disks 

	// transfer disks and display 
	disk.transfer();

	cout << endl;
	return 0;		
}

Function definitions:

#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
}

// function towers contains the algorithm to transfer the disks / pegs, using the 4//parameters of n number of disks to be moved, the peg on which the disks initially start 
//on, the peg to use as use as a temporary holding area, and the peg to which the disks
// will be moved to. Basically the base case is if you need to transfer 1 disk from A to B, 
//then transfer it directly but if 
// you need to transfer n disks from A to B, then there are 3 basic steps, we can transfer n-1
//disks from A to C, then we can 
// transfer the last disk from A to B, and finally transfer the n-1 disks from C to B
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);
	}
}

// function transfer prompts the user for the number of disks, prints the instructions to 
//move the disks, and then allows the user
// to choose from which tower to transfer, then based on the choice it uses a switch 
//statement to pass the parameters to 
// function towers, to apply the algorithm based on the scenario
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;
		//<< "\nChoose from which to which tower to transfer: ";
	//cin >> choice;
	//validateChoice(choice);
	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)
	{
		answer = true;
		cout << "Function passed.";
	}
	else
	{
		answer = false;
		cout << "Function failed.";
	}

	return answer;
}

if(Disks::disks == 1) will work only if the disks is a static variable, i.e.

class Disks
{
   ...
   static int disks;
   ...
};

If it's not feasible to have it as static, then you need an Disks object, i.e.

void some_function(Disks & aDisk)
{
    if(aDisk.disks == 1)
    ...

if(Disks::disks == 1) will work only if the disks is a static variable, i.e.

class Disks
{
   ...
   static int disks;
   ...
};

If it's not feasible to have it as static, then you need an Disks object, i.e.

void some_function(Disks & aDisk)
{
    if(aDisk.disks == 1)
    ...

I'm sorry I'm confused. I think I know what you are saying but I do not know how to implement it. And also why would I have some other functions. And if I have a reference then I need to initialize it.

I'm sorry I'm confused. I think I know what you are saying but I do not know how to implement it. And also why would I have some other functions. And if I have a reference then I need to initialize it.

I don't see a call to the test() function so I don't know what you are trying to do and why. Therefore I can't suggest a way to implement it. I see you have this function as static and not as a member of the Disk class. I can think of ways to make it compile, but since don't understand exactly what you are testing, I don't know what the purpose of the ToH class is and why you have chosen to make the test() function static. If you're not sure why you want to have it be static and you're not sure what static means, I'd make it not be static. And is there any reason you are creating a class to test this rather than just a plain old function?

I don't see a call to the test() function so I don't know what you are trying to do and why. Therefore I can't suggest a way to implement it. I see you have this function as static and not as a member of the Disk class. I can think of ways to make it compile, but since don't understand exactly what you are testing, I don't know what the purpose of the ToH class is and why you have chosen to make the test() function static. If you're not sure why you want to have it be static and you're not sure what static means, I'd make it not be static. And is there any reason you are creating a class to test this rather than just a plain old function?

because we were asked to do this - I need to create another class to use the test method to check the recursion.

And if I have a reference then I need to initialize it.

Following is an usage example of using a function taking the argument by reference

// takes a Disks object by reference
void some_function(Disks & aDisk)
{
    if(aDisk.disks == 1)
    {
        // do something here
    }
}

int main()
{
    Disks obj(123);

    // invoke some_function on obj
    some_function(obj);

    return 0;
}

Following is an usage example of using a function taking the argument by reference

// takes a Disks object by reference
void some_function(Disks & aDisk)
{
    if(aDisk.disks == 1)
    {
        // do something here
    }
}

int main()
{
    Disks obj(123);

    // invoke some_function on obj
    some_function(obj);

    return 0;
}

Ok well I got it to work, however, not using a reference. The following is my current code. However, I do not feel the test is effective because in the function Disks::towers you would figure that disks, and hence finalDisks would end at 1, but instead there are 3 left? Doesn’t make sense if it reached the base case.

Header file:

class Disks
{
public:
	Disks(int);						
	void setVariables(int);			
	//void validateDisks(int);		
	//void validateChoice(int);		
	int towers(int,int,int,int);	
	void transfer();				
private:
	int disks;						
	int choice;						
};

class ToH
{
public:
	static bool test(int);				
};

Main function:

#include "Disks.h"

int main()
{
	// create the object
	Disks disk(3);	// number of disks 

	// transfer disks and display 
	disk.transfer();

	//ToH::test();

	cout << endl;
	return 0;		
}

Function definitions:

#include "Disks.h"			
//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
}

int 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);
	}

	int finalDisks = disks;

	return finalDisks;
}


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:
			disks = towers( disks, 1, 2, 3 );
			break;
		case 2:
			disks = towers( disks, 1, 3, 2 );
			break;
		case 3:
			disks = towers( disks, 2, 1, 3 );
			break;
		case 4:
			disks = towers( disks, 2, 3, 1 );
			break;
		case 5:
			disks = towers( disks, 3, 1, 2 );
			break;
		case 6:
			disks = towers( disks, 3, 2, 1 );
			break;
	}

	ToH::test(disks);  // should still be at 3 (the value initialized 
                                          // to in the object) if all disks were moved
	cout << endl;
}

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

	if(diskResults == 3)   // should still be at 3 (the value initialized 
                                               // to in the object) if all the disks were 
                                               // moved
	{
		answer = true;
		cout << "\nFunction passed.";
	}
	else
	{
		answer = false;
		cout << "\nFunction failed.";
	}

	return answer;
}
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.