Web Hosting Talk







View Full Version : Getting errors in this card program...


Liguidsoul
03-03-2005, 01:40 AM
Three files:

card.h
// Chapter 6 of C++ How to Program
// Debugging Problem (card.h)

#ifndef CARD_H
#define CARD_H

// class card definition
class Card {

public:
void Card();
void Card( int, int );
void ~Card();

void setSuit( int );
int getSuit() const;

void setValue( int );
int getValue() const;

void print() const;

private:
int suit = 4;
int value = 1;
bool validCard() const;

}; // end class Card

#endif // CARD_H

card.cpp

// Chapter 6 of C++ How to Program
// Debugging Problem (card.cpp)

#include <iostream>

using std::cout;
using std::endl;

#include "card.h"

// default constructor
void Card::Card()
{
suit = 4;
value = 1;

} // end class Card constructor

// constructor
Card::Card( int s, int v )
{
suit = s; value = v;

} // end class Card constructor

// destructor
Card::~Card()
{
cout << "The destructor has been invoked\n";

} // end class Card destructor

// set suit
void Card::setSuit( int s )
{
suit = s;

} // end function setSuit

// set value
void Card::setValue( int v )
{
value = v;

} // end function setValue

// function print definition
void print()
{
// is card valid
if ( !validCard() ) {
cout << "This card is not valid\n";
return;

} // end if

cout << "This is the: ";

// determine face of card
switch ( value ) {
case 1:
cout << "Ace ";
break;

case 11:
cout << "Jack ";
break;

case 12:
cout << "Queen ";
break;

case 13:
cout << "King ";
break;

default:
cout << value << " ";

} // end switch

// determine suit
switch ( suit ) {
case 1:
cout << "of clubs\n";
break;

case 2:
cout << "of diamonds\n";
break;

case 3:
cout << "of hearts\n";
break;

case 4:
cout << "of spades\n";
break;

default:
cout << "\ninvalid suit\n";

} // end switch

} // end function print

// return suit
int Card::getSuit()
{
return suit;

} // end function getSuit

// return value
int Card::getValue()
{
return value;

} // end function getValue

// function validCard definition
bool validCard()
{
return value >= 1 && value <= 13 && suit >= 1 && suit <= 4;

} // end function validCard

debugging06.cpp
// Chapter 6 of C++ How to Program
// Debugging Problem

#include <iostream>

using std::cout;
using std::endl;

#include "card.h"

int main()
{
Card c1;
Card c2( 3, 4 );
Card c3( 1, 14 );

Card *p1 = &c2;

c1.print();
c2.print();
c3.print();
p1->print();
cout << endl;

c1.setSuit( p1->getSuit() );
c3.value = 12;
p1->value = 5;

c1.print();
c2.print();
c3.print();
*p1.print();
cout << endl;

return 0;

} // end main

The errors are something along the lines of this:
In file included from debugging06.cpp:9:
card.h:11: return type specification for constructor invalid
card.h:12: return type specification for constructor invalid
card.h:13: return type specification for destructor invalid
card.h:24: ANSI C++ forbids initialization of member `suit'
card.h:24: making `suit' static
card.h:24: ANSI C++ forbids in-class initialization of non-const static member `
suit'
card.h:25: ANSI C++ forbids initialization of member `value'
card.h:25: making `value' static
card.h:25: ANSI C++ forbids in-class initialization of non-const static member `
value'
debugging06.cpp: In function `int main()':
card.h:25: `int Card::value' is private
debugging06.cpp:26: within this context
card.h:25: `int Card::value' is private
debugging06.cpp:27: within this context
debugging06.cpp:32: request for member `print' in `p1', which is of non-aggregat
e type `Card *'

I do not need you to fix every single error... just whatever you can notice off-hand that is obvious. Thanks!

Burhan
03-03-2005, 02:28 AM
Your method signatures for your second (overloaded) constructor do not match. In your class, your second constructor returns void, however when you define it, you omit a return type. Same for your destructor.

You cannot set private properties of classes directly, which is why you are getting debugging06.cpp: In function `int main()':
card.h:25: `int Card::value' is private. Use the setValue() method instead.

You never define the print() method. void print() is not defining a member function of the class. You forgot to include the scope resolution operator (::), and not to mention it doesn't have the correct signature. Check your other methods also.

Liguidsoul
03-03-2005, 02:35 AM
Thanks, I will attempt to make some of the corrections you mention and see if I can get this running soon.

Take care!

error404
03-03-2005, 02:52 AM
Also, in the private section you're attempting to initialise two integers (suit and value). This is only valid if they are made const. Since you also initialise these variables in the constructor, the declaration/initialization is pointless and will actually cause problems due to the syntactic rules of the language. If you want your variables to have object scope (that is, each instance of your class has an independant storage area), you can't initialise them in the declaration, only in the definition (code), though a handy syntax is provided for this.

In any case, remove the initialisations in the declaration, and take fyrestrtr's advice and your code should compile.

Liguidsoul
03-03-2005, 03:03 AM
I was able to solve it by following some of your advice. Thank you for your recomendations.