visual studio 6.0 compiler error

Started by
9 comments, last by cladinshadow 19 years, 10 months ago
quick background: Trying to write a Euchre card game Problem: I googled the error but I got no solutions or explanations. Here is the error (and related errors): error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 those occur in the .cpp file for player, player.cpp ( the source file for player is included and in the source file for player, the source file for card is included ). it occurs in this function, at the line commented.
quote: void player::addCard ( card theCard ) { int j if ( cardsinHand != 0 ){ card tempHand[cardsinHand]; //Yay! an error here! Tell your friends! for ( i = 0; i < cardsinHand; i++ ) { tempHand[j] = hand[j]; } delete hand; hand = new card[++cardsinHand]; for ( i = 0; i < cardsinHand; i++ ) { hand[j] = tempHand[j]; } hand[cardsinHand-1] = theCard; } else { hand = new card[++cardsinHand]; hand[0] = theCard; } }
Here is the source file
quote: #ifndef PLAYER_H #define PLAYER_H #include <string.h> #include "card.h" class player { public: player ( ); player ( char* name, char* team ); ~player ( ); void addCard ( card theCard ); void removeCard ( int theCard ); card* showHand ( ); card showCard( ); void addTrick ( ); int getTricks ( ); char* getName( ); void setName( char* theName ); char* getTeam( ); void setTeam( char* theTeam ); private: char* name; card* hand; char* team; int tricks; int cardsinHand; }; #endif
lemme know if you know the solution or need more info. thanks for yer time! [edited by - cladinshadow on June 3, 2004 6:20:34 PM] [edited by - cladinshadow on June 3, 2004 6:21:23 PM]
_______________________This post brought to youby the number12...and Darren
Advertisement
You can't create an array like that with a variable specifying the number of elements.

Arrays need to have a constant number of members, so you could do something like this:

#define MAXCARDS 7 // or however many you get in euchre
...
card tempHand[MAXCARDS];

or you could create he array on the fly

card *tempHand = new card[cardsinHand];

and make sure to delete that later

or you could use some of the container classes that provide dynamic arrays.


[edited by - DerekZahn on June 3, 2004 6:34:14 PM]
In fact, looking at the code, this is a simple way to do it:
void player::addCard ( card theCard ) {  int j;  card *tempHand = new card[cardsinHand+1];  for(j = 0; j < cardsinHand; j++)    tempHand[j] = hand[j];  tempHand[cardsinHand++] = theCard;  delete hand;  hand = tempHand;}


Alot of strange things going on in your code, well your getting that error because you can''t specifiy the size of a statically allocated array with a non constant variable in this case the variable "cardsinHand".

You''ve declared a local variable "j", don''t assign any values to it and your trying to you use as a loop control variable so when your doing "tempHand[j] = hand[j];" in that loop(s) your just re-assigning the same value in the same element of hand to the same element of tempHand what ever the value of j is initialzied to.

Also you declared the data member "hand" to be of type pointer to char but then your assign an instance of type Card. You should also post your Card type.

Another thing your using C++ but your including C style headers you should change it from "#include <string.h>" to "#include <cstring> but you might be better off using standard library string instead of arrays of characters.

Its really hard to tell what your trying to achieve in that function and i think your making it more complicated than it really is. By the looks of things you want a container of Cards and from looking at "addCard" method it looks like you''ll want a list of Cards. I would change it to something like this:

#include "Card.hpp"#include <string>#include <list>class Player {public:   Player();   Player(const std::string&, const std::string&);   Player(const Player&); //copy-constructor   Player& operator=(const Player&); //asignment ops   const bool addCard(const Card& theCard);   const bool removeCard(int index);      const Card& showHand() const;   Card& showHand();   const Card& showCard() const;   Card& showCard();   void addTrick();   int getTricks();   const std::string& getName() const;   std::string& getName();   void setName(const std::string& theName);   const std::string& getTeam() const;   std::string& getTeam();   void setTeam(const std::string& theTeam);private:   std::string name, team;   std::list<Card> hand;   int tricks;   int cardsinHand;};


then that function would be something like:

const bool addCard(const Card& theCard) {    //... do something    hand.push_back(theCard);    return true;}
Thats why it look strange, you''ve put your code around a quote tag and its not displaying properly on my web browser.
snk_kid:

* he used i instead of j by mistake in the loop
* despite what you say, 'hand' is declared as a 'card *' in his code
* it's perfectly fine to use functions from string.h in a c++ program

However, the use of a dynamic array as you demonstrate would also be a fine way to do this, it just requires a complete redesign of the data structures.


[edited by - DerekZahn on June 3, 2004 9:21:59 PM]
quote:Original post by DerekZahn
snk_kid:

* he used i instead of j by mistake in the loop
* despite what you say, ''hand'' is declared as a ''card *'' in his code
* it''s perfectly fine to use functions from string.h in a c++ program

However, the use of a dynamic array as you demonstrate would also be a fine way to do this, it just requires a complete redesign of the data structures.


[edited by - DerekZahn on June 3, 2004 9:21:59 PM]


I''ve already mentioned that the code is not displaying properly on my browser it shows hand declared as pointer to char.

It may be fine to use string.h on his compiler but thats going against the standard why would you do that? Technically a pure c++ compiler shouldn''t even have a header called that. Also if it doesn''t support any version of C and it does have that header whos to say that its beening maintained anymore you can''t be sure for all the compilers in the world. VC++ .NET 2003 will through errors at that because its following that standard more by taking those headers out!. If he was using C then thats totally standard for that language but its not for C++
Just trying to answer his question without forcing a religion on him and making him rewrite his whole program from scratch.

and

#include <cstring> is better, but
#include <string.h> is legal, per ansi c++

You'll get less confusion if you find a browser that doesn't replace the word 'card' with the word 'char' at random.


[edited by - DerekZahn on June 4, 2004 10:11:07 AM]
normally in C++, to use the string header from the C standard library, you'd put

#include <cstring>

and to use C++' very own string library you'd put

#include <string>

and refer to functions and classes with std:: or by bringing the namespace into scope with a using declaration. e.g.

std::string name("quorn");

//or

using std::string;

string website("www.quorn.com");

[edited by - quorn on June 4, 2004 10:14:54 AM]
quote:Original post by DerekZahn
Just trying to answer his question without forcing a religion on him and making him rewrite his whole program from scratch.

and

#include <cstring> is better, but
#include <string.h> is legal, per ansi c++



What are you talking about religion? string.h is not in the ansi/ISO standard for C++ language, its not technically valid. There is quite a bit of a difference between C and C++ especially C99 your getting them mixed up.

quote:
You''ll get less confusion if you find a browser that doesn''t replace the word ''card'' with the word ''char'' at random.


Well if he put his code in source/code tags instead of quotes!?! in the first place people wouldn''t get mixed up.

This topic is closed to new replies.

Advertisement