我的card类不断出现编译器错误c1001

my card class keeps getting a compiler error c1001

本文关键字:编译器 错误 c1001 card 我的      更新时间:2023-10-16

这是标题

#ifndef CARD_H
#define CARD_H
#include <iostream>
#include <ios>
#include <string>
using namespace std;
class Card {
friend class deck;
friend class Hand;
private:
// assigns ranks to a string to greatly shorten the necessary array
const string ourRanks[13] = { "2", "3", "4",
    "5", "6", "7", "8", "9", "10", "Jack",
    "Queen", "King", "Ace" };
//  assigns suits to a string to greatly shorten the necessary array
const string ourSuits[5] = { "Clubs", "Diamonds", "Hearts", "Spades" };

// stores the rank
int rank;
// stores the suit
int suit;
// card, stores string card for usage
string card;

public:

Card() {
    this -> rank = 0;
    this -> suit = 0;
    this -> card = "The Two of clubs";
    return;
}

// Card constructor
Card(int ranks, int suits);

// gets rank and suit
int getRank(int ranks);
int getSuit(int suits);

// defines the value
int getValue();

// gets the highest and lowest values
int getHighValue();
int getLowValue();

// finds and declares the rank and suit
bool validRank(int ranks);
bool validSuit(int suits);

// creates a card
 bool equals();

// exports the card to be used
string newCard();
// These create the numerical representation of the card ranks
const int TWO = 0;
const int THREE = 1;
const int FOUR = 2;
const int FIVE = 3;
const int SIX = 4;
const int SEVEN = 5;
const int EIGHT = 6;
const int NINE = 7;
const int TEN = 8;
const int JACK = 9;
const int QUEEN = 10;
const int KING = 11;
const int ACE = 12;

// These create the numerical representation of the card suits
const int CLUB = 0;
const int DIAMONDS = 1;
const int HEARTS = 2;
const int SPADES = 3;
 };
  #endif

这是cpp文件

  #include <iostream>
  #include <ios>
  #include <cmath>
  #include <string>
  #include <assert.h>
  #include <stdio.h>

  using namespace std;
  #include "Card.h"

  Card::Card(int ranks, int suits) {
  this -> rank = getRank(ranks);
  this -> suit = getSuit(suits);
  assert(validSuit(suits));
  assert(validRank(ranks));
  this->card = newCard();
  return;
  }


  int Card :: getRank(int ranks) {
  rank = ranks;
  return  rank;
  }

  int Card :: getSuit(int suits) {
  suit = suits;
  return suit;
   }

  int Card ::getValue() {
  if (rank == TWO) {
    return 2;
  }
  else if (rank == THREE) {
    return 3;
  }
  else if (rank == FOUR) {
    return 4;
  }
  else if (rank == FIVE) {
    return 5;
  }
  else if (rank == SIX) {
    return 6;
  }
   else if (rank == SEVEN) {
    return 7;
  }
  else if (rank == EIGHT) {
    return 8;
  }
  else if (rank == NINE) {
    return 9;
  }
  else if (rank == TEN) {
    return 10;
  }
  else if (rank > TEN && rank != ACE) {
    return 10;
  }
  else {
    return -1;
  }
  }

  int Card ::getHighValue() {
  int high = getValue();

   if (high == -1) {
    return 11;
   }
   else {
    return high;
    }
    }

    int Card ::getLowValue() {
    int low = getValue();
    if (low == -1) {
    return 1;
    }
    else {
    return low;
    }
    }

    bool Card ::validRank(int ranks) {
    if ((TWO <= ranks) && (ACE >= ranks)) {
    return true;
    }
    else {
    return false;
    }
    }

     bool Card ::validSuit(int suits) {
     if ((CLUB <= suit) && (SPADES >= suits)) {
     return true;
     }
     else {
     return false;
     }
     }
     bool Card :: equals() {
     Card otherCard(rank, suit);
     if ((suit == otherCard.suit) && (rank == otherCard.rank)) {

     return true;
     }
     else {
     return false;
     }
     }
     string Card::newCard() {
     card = ourRanks[getRank(rank)];
     card.append(" of ");
     card.append(ourSuits[getSuit(suit)]);
     return  card;
     }

错误如下:

Severity    Code    Description Project File    Line
Error   C1001   An internal error has occurred in the compiler. {Project4}  c:usersthelazyogredocumentsvisual studio 2015projects{project4}card.cpp  23

很抱歉浪费你的时间,我解决了问题,它现在可以正常工作了,我习惯了对我的数组进行多次初始化,我通过重新调整它的参数的来修复它

这是一个编译器错误。我在VS2015中尝试了相同的代码,并收到消息:

1>------ Rebuild All started: Project: scratch, Configuration: Debug Win32 ------
1>  scratch.cpp
1>E:CodeVS2015scratchscratchscratch.cpp : fatal error C1001: An internal error has occurred in the compiler.
1>  (compiler file 'f:ddvctoolscompilerutcsrcp2main.c', line 246)
1>   To work around this problem, try simplifying or changing the program near the locations listed above.
1>  Please choose the Technical Support command on the Visual C++
1>   Help menu, or open the Technical Support help file for more information
1>
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

正如Patrick Duggin的回答和评论中所指出的,它与数组初始化有关。我的最小可复制性如下:

#include <string>
class X
{
    std::string strings[2] = { "first" };
};
int main()
{
    X x;
}

上面的代码会产生相同的错误。这是有效的C++代码。调整数组大小或向数组添加另一个初始化程序是解决该错误的方法。

我无法使用不同类型的数组来重现错误。我没有动力去弄清楚std::string到底是什么导致了这种情况。