C++编译报错:'const char*'到'char'无效转换 求排查与用法讲解
错误成因与解决说明
核心语法说明
const char*是指向常量字符的指针,是C/C++中表示C风格字符串的标准类型:
- 所有双引号包裹的字符串字面量(如
"Hearts")的默认类型就是const char*,指向存储字符串的内存首地址 const限定表示指针指向的字符串内容不可修改,避免意外改写字符串常量- 单个
char类型只能存储一个字符(如'A',单引号包裹),无法存储完整字符串,也不能接收const char*类型的传参,这就是你遇到编译错误的直接原因
具体代码修改点
- 修正类内成员方法的参数/返回值类型:
- 构造函数
Card(int, char);改为Card(int, const char*); setCard(int, const char);改为void setCard(int, const char*);const char getSuit();改为const char* getSuit();
- 构造函数
- 带参构造函数的实现修正:第二个参数类型改为
const char*,调用setCard时不要重复声明参数类型,直接传参即可 - 类外实现成员方法必须加
Card::前缀,你的setCard和displayCard实现前都漏了该前缀,会被识别为全局函数无法访问类私有成员 - 修正
setCard内的逻辑错误:原花色判断逻辑完全错误,改为将传入的newSuit分别和四个合法花色比较,默认值直接写常量字符串"Hearts"即可
修正后完整可运行代码
#include <iostream> #include <iomanip> #include <cstdlib> #include <cstring> using namespace std; class Card { public: Card(); Card(int, const 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, const char* suit) { setCard(face, suit); } void Card::setCard(int newFace, const char* newSuit) { int hearts, diamonds, spades, clubs; if(newFace > 0 && newFace < 14) { faceValue = newFace; } else { faceValue = 1; } hearts = strcmp(newSuit, "Hearts"); diamonds = strcmp(newSuit, "Diamonds"); spades = strcmp(newSuit, "Spades"); clubs = strcmp(newSuit, "Clubs"); if(hearts == 0 || diamonds == 0 || spades == 0 || clubs == 0) { strcpy(suitValue, newSuit); } else { strncpy(suitValue, "Hearts", 9); } } int Card::getFace() { return faceValue; } const char * Card::getSuit() { return suitValue; } void Card::displayCard() { switch(faceValue) { case 1: cout << "Ace of " << suitValue; break; case 11: cout << "Jack of " << suitValue; break; case 12: cout << "Queen of " << suitValue; break; case 13: cout << "King of " << suitValue; break; default: cout << faceValue << " of " << suitValue; } }
内容的提问来源于stack exchange,提问作者Fortnite_Pro
相关产品推荐
相关产品推荐

