| | |
Constructor Error
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
Card is the name of one of my classes. Here is how I am using the constructor in my driver program:
Here is the constructor I am trying to invoke with above code:
Can anyone help me find the problem with this statement? Thank you for any help in advance.
Nick
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
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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
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
Card.cpp
Card.h
Queue.h
Queue.cpp
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
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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
Nick
![]() |
Similar Threads
- Problem receiving result from Microsoft Soap toolkit server service using Nusoap clie (RSS, Web Services and SOAP)
- Does anyone have the stomach to look over my program? (C++)
- check for errors in my code (C++)
Other Threads in the C++ Forum
- Previous Thread: MFC - OnKeyDown event.
- Next Thread: OpenGL in VC++ 2005
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





