Hi everyone,
I am currently facing a problem with a program that I am writing. It is a computer simulation of the card game War. I am receiving this error
main.cpp:22: invalid conversion from `Card*' to `int'
main.cpp:22: initializing argument 1 of `Card::Card(int)'
Card is the name of one of my classes. Here is how I am using the constructor in my driver program:
Card deck[52]; //deck of cards - standard, no jokers
Queue p1, p2; // player1, player2
for (int i=0; i<52; ++i)
deck[i] = new Card[i];
Here is the constructor I am trying to invoke with above code:
Card::Card(int f)
{
face = f; // the value of the card (between 0&51)
}
Can anyone help me find the problem with this statement? Thank you for any help in advance.
Nick
Yes, you had better sending your entire code. It's impossible to understand a program with just a piece of it.
Thank you for the help. I believe I have fixed the problem by making a function instead of using a constructor. I appreciate all of your help. I do, however have an entirely new problem with this program. It will compil ok, but then when it get's to my algorithm where it should shuffle the deck of cards, I get a segmentation fault. Any ideas on this? I have included all of my code below and highlighted in red where I am getting the seg fault. Thank you for your help in advance.
Nick
main.cpp
#include<iostream> // obviousely
using namespace std;
#include <iomanip> // for formatting
#include <ctime> // to seed random number generator with the time
#include "Card.h" // my Card object
#include "Queue.h" // my Queue class
int main()
{
Card deck[52]; //deck of cards - standard, no jokers
Queue p1, p2; // player1, player2
cout << "deck and p1, p2 created." << endl;
for (int i=0; i<52; ++i)
deck[i].setvalue(i);
cout << "deck given 0-51 values" << endl;
srand(time(0)); // seed random number generator
for (int i=0; i<51; ++i)
{
int swapIndex = (rand() % 52) + 1;
Card tmp = deck[swapIndex];
deck[swapIndex] = deck[i];
deck[i] = tmp;
}
cout << "deck shuffled" << endl;
for (int i=0; i<26; ++i)
p1.enqueue(deck[i]);
for (int i=26; i<52; ++i)
p2.enqueue(deck[i]);
cout << "deck divided between players" << endl;
return 0;
} Card.cpp
#include <iostream>
using namespace std;
#include "Card.h"
Card::Card(void)
{
face = 0;
}
Card::~Card(void)
{
delete this;
}
Card::Card(const Card &c)
{
copy(c);
}
void Card::setvalue(int f)
{
face = f;
}
bool Card::operator> (const Card &c)
{
bool rval = false;
if ((face % 13) > (c.face % 13))
rval = true;
return rval;
}
bool Card::operator< (const Card &c)
{
bool rval = false;
if ((face % 13) < (c.face % 13))
rval = true;
return rval;
}
const Card & Card::operator= (const Card &c)
{
copy(c);
}
void Card::copy(const Card &c)
{
face = c.face;
}
ostream & operator<< (ostream &ost, const Card &c)
{
switch ( (c.face % 13) )
{
case 0:
ost << "Ace ";
break;
case 1:
ost << "Two ";
break;
case 2:
ost << "Three ";
break;
case 3:
ost << "Four ";
break;
case 4:
ost << "Five ";
break;
case 5:
ost << "Six ";
break;
case 6:
ost << "Seven ";
break;
case 7:
ost << "Eight ";
break;
case 8:
ost << "Nine ";
break;
case 9:
ost << "Ten ";
break;
case 10:
ost << "Jack ";
break;
case 11:
ost << "Queen ";
break;
case 12:
ost << "King ";
break;
}
ost << "of ";
switch ( (c.face / 13) )
{
case 0:
ost << "Hearts";
break;
case 1:
ost << "Spades";
break;
case 2:
ost << "Clubs";
break;
case 3:
ost << "Diamonds";
break;
}
return ost;
} Card.h
#ifndef CARD_H
#include <iostream>
using namespace std;
class Card
{
private:
int face;
public:
// int face;
Card(void);
~Card(void);
Card(const Card &);
void setvalue(int);
bool operator> (const Card &);
bool operator< (const Card &);
const Card & operator= (const Card &);
void copy(const Card &);
friend ostream & operator<< (ostream & ost, const Card &);
};
#endif Queue.h
#ifndef QUEUE_H
class Queue
{
private:
int _size;
Card *arr;
int used;
public:
Queue(void);
~Queue(void);
Queue(const Queue &);
// const Queue& operator= (const Queue &);
void enqueue(const Card &);
Card dequeue(void);
int size(void) const;
bool empty(void) const;
private:
void copy(const Queue &);
};
#endif Queue.cpp
#include <iostream>
using namespace std;
#include "Card.h"
#include "Queue.h"
Queue::Queue(void)
{
_size = 10;
arr = new Card[_size];
used = 0;
}
Queue::~Queue(void)
{
delete []arr;
}
bool Queue::empty(void) const
{
return (used <= 0);
}
int Queue::size(void) const
{
return used;
}
void Queue::enqueue(const Card &c)
{
if (used >= _size)
{
int nsize = _size+5;
Card *narr = new Card[nsize];
for (int i=0; i<used; ++i)
narr[i] = arr[i];
delete []arr;
arr = narr;
_size = nsize;
}
arr[used++] = c;
}
Card Queue::dequeue(void)
{
Card rval;
if ( empty() )
cerr << "dequeue when empty error" << endl;
else
{
rval = *arr;
for (int i=1; i<used; ++i)
arr[i-1] = arr[i];
--used;
}
return rval;
}
void Queue::copy(const Queue &q)
{
_size = q._size;
used = q.used;
arr = new Card[_size];
for (int i=0; i<used; ++i)
arr[i] = q.arr[i];
}
Queue::Queue(const Queue &q)
{
copy(q);
}I believe I figured it out. I think I was improperly destructing my Card object, therefore it was running out of memory because it was not being destructed. I got the code I have to run completely without any other errors when i commented out my destructor and just went with a default destructor for this object. Thank you for the help!
Nick