I have been stuck on this for over a week trying to just get the basics of this to work. I'm having real trouble getting the constructor to work properly or even at all.
Here is my instructor provided header file which I am unable to edit
// File: Peg.h
/// Class Peg from Main and Savage
//
// provides a "peg" that can hold a maximium of 64 disks
// of sizes 1 to MAX_INT in diameter.
//
// CONSTRUCTOR: Peg(x)
// POSTCONDITION the Peg has been initialized to have x disks of size x to size 1
// on the peg
//
// CONSTANT functions:
//
// size_t count ();
// POSTCONDITIONS: the number of disks on the Peg is returned
//
// Disk top_Disk ();
// PRECONDITIONS: the Peg is not empty
// POSTCONDITIONS: the disk at the top of the Peg is returned
//
//
// MODIFICATION functions:
//
// void add (const Disk added_Disk);
// PRECONDITIONS: the Peg is not full AND the added_Disk is smaller than the top disk
// POSTCONDITIONS: the Peg has the disk added to the top of the peg
//
// void remove ();
// PRECONDITIONS: the Peg is not empty
// POSTCONDITIONS: the top disk is removed from the Peg
//
// FRIENDS
// operator <<
// POSTCONDITIONS: the disk on the peg are printed from the bottom to the top
//
#define MAX_DISKS 64
#include <cstdlib>
#include <cassert>
#include <iostream>
class Peg
{
public:
typedef std::size_t Disk;
Peg(std::size_t init_disks=64);
std::size_t count() const;
Disk top_Disk() const;
void add(const Disk added_Disk);
void remove ();
friend std::ostream& operator<< (std::ostream&, const Peg&);
private:
std::size_t size;
std::size_t disks[MAX_DISKS];
};
So far all it does is output a "26" or what ever is in the constructor of the test file "Peg peg(26)"
It if compiles, you haven't presented us with the source code you're using. There is a syntax error in your Peg constructor. Variables of type std::size_t don't have members named size. You also don't set the class member size anywhere in the constructor.
I would expect the constructor's implementation to look like the following:
1 2 3 4 5 6 7 8 9
// User enters how many pegs they wish to start with
Peg::Peg(std::size_t init_disks) : size(init_disks)
{
if ( size > MAX_DISKS )
size = MAX_DISKS ;
for (unsigned i=0; i<size; ++i)
disks[i] = size-i ;
}
Thank you kindly so far
For awhile I couldn't figure out how to set the size, I herp derped the whole time and was doing it wrong.
In the constructor how would I make it so that if this happened Peg peg()
I need to make if this happens that it gets set to 64
Thought about adding another constructor to the header file with an empty parameter list, but I am not able to change the header file.
thought about doing if (size > MAX_DISKS || NULL)
But I know NULL are not allowed here, what else could I use?
I need to make if this happens that it gets set to 64
Thought about adding another constructor to the header file with an empty parameter list, but I am not able to change the header file.
That's already taken care of by the default argument supplied in the method's declaration in Peg.h.
[Edit: Although, if you want a default constructed object you should drop the parentheses. Peg peg(); is a function named peg that returns a variable of type Peg. ]
You cire be genius
one last thing then I'll go back to trying it on my own
How can I display the size at a certain point within the array?
I know this won't work disks[i].size
[Edit: nor cout << peg.size[1];
error: invalid types /size_t[int]/ for array subscript ]