Constructor Error

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Constructor Error

 
0
  #1
Mar 3rd, 2006
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

  1. main.cpp:22: invalid conversion from `Card*' to `int'
  2. main.cpp:22: initializing argument 1 of `Card::Card(int)'
  3.  

Card is the name of one of my classes. Here is how I am using the constructor in my driver program:

  1. Card deck[52]; //deck of cards - standard, no jokers
  2. Queue p1, p2; // player1, player2
  3.  
  4.  
  5. for (int i=0; i<52; ++i)
  6. deck[i] = new Card[i];

Here is the constructor I am trying to invoke with above code:

  1. Card::Card(int f)
  2. {
  3. face = f; // the value of the card (between 0&51)
  4. }

Can anyone help me find the problem with this statement? Thank you for any help in advance.
Nick
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: Constructor Error

 
0
  #2
Mar 3rd, 2006
it would probably be easier is u could post all ur code then we can look at it all.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Constructor Error

 
0
  #3
Mar 3rd, 2006
You're treating deck[i] like a pointer when it's not.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 71
Reputation: AhmedHan is an unknown quantity at this point 
Solved Threads: 1
AhmedHan's Avatar
AhmedHan AhmedHan is offline Offline
Junior Poster in Training

Re: Constructor Error

 
0
  #4
Mar 4th, 2006
Yes, you had better sending your entire code. It's impossible to understand a program with just a piece of it.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: Constructor Error

 
0
  #5
Mar 5th, 2006
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
  1. #include <iostream>
  2. using namespace std;
  3. #include "Card.h"
  4.  
  5.  
  6. Card::Card(void)
  7. {
  8. face = 0;
  9. }
  10.  
  11. Card::~Card(void)
  12. {
  13. delete this;
  14. }
  15.  
  16. Card::Card(const Card &c)
  17. {
  18. copy(c);
  19. }
  20.  
  21.  
  22. void Card::setvalue(int f)
  23. {
  24. face = f;
  25. }
  26.  
  27. bool Card::operator> (const Card &c)
  28. {
  29. bool rval = false;
  30. if ((face % 13) > (c.face % 13))
  31. rval = true;
  32. return rval;
  33. }
  34.  
  35. bool Card::operator< (const Card &c)
  36. {
  37. bool rval = false;
  38. if ((face % 13) < (c.face % 13))
  39. rval = true;
  40. return rval;
  41. }
  42.  
  43. const Card & Card::operator= (const Card &c)
  44. {
  45. copy(c);
  46. }
  47.  
  48. void Card::copy(const Card &c)
  49. {
  50. face = c.face;
  51. }
  52.  
  53. ostream & operator<< (ostream &ost, const Card &c)
  54. {
  55. switch ( (c.face % 13) )
  56. {
  57. case 0:
  58. ost << "Ace ";
  59. break;
  60. case 1:
  61. ost << "Two ";
  62. break;
  63. case 2:
  64. ost << "Three ";
  65. break;
  66. case 3:
  67. ost << "Four ";
  68. break;
  69. case 4:
  70. ost << "Five ";
  71. break;
  72. case 5:
  73. ost << "Six ";
  74. break;
  75. case 6:
  76. ost << "Seven ";
  77. break;
  78. case 7:
  79. ost << "Eight ";
  80. break;
  81. case 8:
  82. ost << "Nine ";
  83. break;
  84. case 9:
  85. ost << "Ten ";
  86. break;
  87. case 10:
  88. ost << "Jack ";
  89. break;
  90. case 11:
  91. ost << "Queen ";
  92. break;
  93. case 12:
  94. ost << "King ";
  95. break;
  96.  
  97.  
  98. }
  99.  
  100. ost << "of ";
  101.  
  102. switch ( (c.face / 13) )
  103. {
  104. case 0:
  105. ost << "Hearts";
  106. break;
  107. case 1:
  108. ost << "Spades";
  109. break;
  110. case 2:
  111. ost << "Clubs";
  112. break;
  113. case 3:
  114. ost << "Diamonds";
  115. break;
  116. }
  117.  
  118. return ost;
  119. }


Card.h
  1. #ifndef CARD_H
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. class Card
  7. {
  8. private:
  9. int face;
  10.  
  11. public:
  12. // int face;
  13. Card(void);
  14. ~Card(void);
  15. Card(const Card &);
  16. void setvalue(int);
  17. bool operator> (const Card &);
  18. bool operator< (const Card &);
  19. const Card & operator= (const Card &);
  20. void copy(const Card &);
  21. friend ostream & operator<< (ostream & ost, const Card &);
  22. };
  23.  
  24. #endif


Queue.h
  1. #ifndef QUEUE_H
  2.  
  3. class Queue
  4. {
  5. private:
  6. int _size;
  7. Card *arr;
  8. int used;
  9.  
  10. public:
  11. Queue(void);
  12. ~Queue(void);
  13. Queue(const Queue &);
  14. // const Queue& operator= (const Queue &);
  15. void enqueue(const Card &);
  16. Card dequeue(void);
  17. int size(void) const;
  18. bool empty(void) const;
  19.  
  20. private:
  21. void copy(const Queue &);
  22. };
  23.  
  24. #endif


Queue.cpp
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include "Card.h"
  5. #include "Queue.h"
  6.  
  7. Queue::Queue(void)
  8. {
  9. _size = 10;
  10. arr = new Card[_size];
  11. used = 0;
  12. }
  13.  
  14. Queue::~Queue(void)
  15. {
  16. delete []arr;
  17. }
  18.  
  19. bool Queue::empty(void) const
  20. {
  21. return (used <= 0);
  22. }
  23.  
  24. int Queue::size(void) const
  25. {
  26. return used;
  27. }
  28.  
  29. void Queue::enqueue(const Card &c)
  30. {
  31. if (used >= _size)
  32. {
  33. int nsize = _size+5;
  34. Card *narr = new Card[nsize];
  35.  
  36. for (int i=0; i<used; ++i)
  37. narr[i] = arr[i];
  38. delete []arr;
  39. arr = narr;
  40. _size = nsize;
  41. }
  42.  
  43. arr[used++] = c;
  44. }
  45. Card Queue::dequeue(void)
  46. {
  47. Card rval;
  48.  
  49. if ( empty() )
  50. cerr << "dequeue when empty error" << endl;
  51. else
  52. {
  53. rval = *arr;
  54.  
  55. for (int i=1; i<used; ++i)
  56. arr[i-1] = arr[i];
  57. --used;
  58. }
  59.  
  60. return rval;
  61. }
  62.  
  63. void Queue::copy(const Queue &q)
  64. {
  65. _size = q._size;
  66. used = q.used;
  67. arr = new Card[_size];
  68.  
  69. for (int i=0; i<used; ++i)
  70. arr[i] = q.arr[i];
  71. }
  72.  
  73. Queue::Queue(const Queue &q)
  74. {
  75. copy(q);
  76. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: Constructor Error

 
0
  #6
Mar 5th, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC