【发布时间】:2021-12-01 14:33:04
【问题描述】:
由于 const char * 的错误,我在编译程序时遇到问题。这是我第一次使用它,我无法绕过它。即使这不是代码的问题,也有人可以链接或解释 const char *?谢谢!
我尝试将不同的 char 数据类型更改为 const char,但这不起作用。在发布此内容之前,我曾尝试在不同的领域进行查找,但我发现没有一个正在使用方法。
代码如下:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
class Card
{
public:
Card();
Card(int, char);
void setCard(int, const char);
int getFace();
const char getSuit();
void displayCard();
private:
int faceValue;
char suitValue[9];
};
int main()
{
//Set the seed value for the random number generator
srand(11);
//Test 1: Constructors and displayCard method
//Create the 3 Card objects
Card firstCard;
Card secondCard;
Card thirdCard = Card( 13, "Spades" );
//Display the 3 objects
cout << "Test 1: Constructors and displayCard method" << endl << endl
<< " The first card is the ";
firstCard.displayCard();
cout << "." << endl << endl << " The second card is the ";
secondCard.displayCard();
cout << "." << endl << endl << " The third card is the ";
thirdCard.displayCard();
//Test 2: setCard method on the first object
cout << endl << endl << endl << "Test 2: setCard method" << endl << endl;
//Change the first card to the Jack of Hearts
firstCard.setCard( 11, "Hearts" );
cout << " Change the first card to the Jack of Hearts. It is now the ";
firstCard.displayCard();
//Test 3: setCard method on the second object
cout << endl << endl << endl << "Test 3: setCard method with invalid values" << endl << endl;
//Try to change the second card using face of -5 and suit of ""
secondCard.setCard( -5, "" );
cout << " The second card should be the Ace of Hearts. It is now the ";
secondCard.displayCard();
//Try to change the first card using face of 16 and suit of "Candles"
firstCard.setCard( 16, "Candles" );
cout << endl << endl << " The first card should be the Ace of Hearts. It is now the ";
firstCard.displayCard();
//Test 4: getFace and getSuit methods on the third object
//Change the third card to 3 of Clubs
thirdCard.setCard( 3, "Clubs" );
cout << endl << endl << endl << "Test 4: getFace and getSuit methods" << endl << endl;
cout << " The third card should have a face value of 3. It is "
<< thirdCard.getFace() << endl
<< " The suit value should be Clubs. It is " << thirdCard.getSuit() << endl;
return 0;
}
Card::Card()
{
faceValue = (rand() % 13) + 1;
int stringValue = (rand() % 4) + 1;
switch(stringValue)
{
case 1:
strcpy(suitValue, "Clubs");
break;
case 2:
strcpy(suitValue, "Diamonds");
break;
case 3:
strcpy(suitValue, "Hearts");
break;
case 4:
strcpy(suitValue, "Spades");
break;
default:
cout << "Your string random number generator is broken";
}
}
Card::Card(int face, char suit)
{
setCard(int newFace, const char newSuit[]);
}
void setCard(int newFace, const char newSuit[])
{
int hearts, diamonds, spades, clubs;
if(newFace > 0 and newFace < 14)
{
faceValue = newFace;
}
else
{
faceValue = 1;
}
hearts = strcmp(suitValue, newSuit);
diamonds = strcmp(suitValue, newSuit);
spades = strcmp(suitValue, newSuit);
clubs = strcmp(suitValue, newSuit);
if(hearts == 0 or diamonds == 0 or spades == 0 or clubs == 0)
{
strcpy(suitValue, newSuit);
}
else
{
strncpy(suitValue, newSuit["Hearts"], 9);
}
}
int Card::getFace()
{
return faceValue;
}
const char * Card::getSuit()
{
return suitValue;
}
void displayCard()
{
switch(faceValue)
{
case 1:
cout << "Ace of " << suitValue << endl;
break;
case 11:
cout << "Jack of " << suitValue << endl;
break;
case 12:
cout << "Queen of " << suitValue << endl;
break;
case 13:
cout << "King of " << suitValue << endl;
break;
default:
cout << faceValue << " of " << suitValue << endl;
}
}
【问题讨论】:
-
编译器会告诉你这个错误发生在哪一行。您发布了太多代码,并且没有练习良好的缩进,使代码更难阅读。
-
您的
void setCard(int, const char);函数在第二个参数中采用单个字符而不是字符串文字,因此firstCard.setCard( 16, "Candles" );将不起作用。它需要firstCard.setCard( 16, 'C' ); -
newSuit["Hearts"]无效,您只需要"Hearts"吗? -
我能够通过将类更改为: `` class Card { public: Card(); 来解决很多问题。卡(int,常量字符[]);无效 setCard(int, const char[]); int getFace(); const char * getSuit();无效的显示卡();私人:int faceValue;字符套装值[9]; }; ``` 其余的问题都是我在尝试修复之前的错误时不小心忽略的小问题