2017-09-27 2 views
0

現在、クラスのカードプログラムで作業しています。コンパイラが何かが宣言されていないという問題が発生しています。スコープは存在し、いくつかのことは全く宣言されていません。ここでは、コードは次のようになります。さらに別の 'このスコープで宣言されていません'という問題が宣言されたとき

Card.h:

#ifndef _CARD_H 
#define _CARD_H 

#include <iostream> 
#include <string> 

using namespace std; 

enum RANK{Joker, Ace = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King} 
enum SUIT{Clubs, Diamonds, Hearts, Spades} 

class Card 
{ 
private: 
    //Rank and Suit variables for all cards 
    int rank; 
    int suit; 

public: 
    //Constructors 
    Card(); 
    Card(int r, int s); 

    //Getters 
    int getRank(); 
    int getSuit(); 

    //Setters 
    void setRank(int r); 
    void setSuit(int s); 

    //toString 
    string toString(); 
}; 

#endif 

Card.cpp:

#ifndef _CARD_H 
#define _CARD_H 
#include "Card.h" 
#include <iostream> 
#include <string> 

using namespace std; 

//Default constructor 
Card::Card() 
{ 
    rank=Joker; 
    suit=Clubs; 
} 

//Constructor 
Card::Card(int r, int s) 
{ 
    rank = r; 
    suit = s; 
} 

//Getters for rank and suit 
int Card::getRank() 
{ 
    return rank; 
} 
int Card::getSuit() 
{ 
    return suit; 
} 

//Setters for rank and suit 
void Card::setRank(int r) 
{ 
    rank = r; 
} 
void Card::setSuit(int s) 
{ 
    suit = s; 
} 

//toString function for output 
string Card::toString() 
{ 
    string tempstring = ""; //list of if-else statements for what to add to the string that gets printed 
    if (rank == 0) 
    { 
     tempstring += "Joker"; 
     goto stringEnd; //sends the process to the end of the list if rank is Joker so it doesn't attempt to add a suit to the card toString 
    } 
    else if (rank == 1) 
     tempstring += "Ace of "; 
    else if (rank == 2) 
     tempstring += "Two of "; 
    else if (rank == 3) 
     tempstring += "Three of "; 
    else if (rank == 4) 
     tempstring += "Four of "; 
    else if (rank == 5) 
     tempstring += "Five of "; 
    else if (rank == 6) 
     tempstring += "Six of "; 
    else if (rank == 7) 
     tempstring += "Seven of "; 
    else if (rank == 8) 
     tempstring += "Eight of "; 
    else if (rank == 9) 
     tempstring += "Nine of "; 
    else if (rank == 10) 
     tempstring += "Ten of "; 
    else if (rank == 11) 
     tempstring += "Jack of "; 
    else if (rank == 12) 
     tempstring += "Queen of "; 
    else if (rank == 13) 
     tempstring += "King of "; 
    if (suit == 0) 
     tempstring += "Clubs"; 
    else if (suit == 1) 
     tempstring += "Diamonds"; 
    else if (suit == 2) 
     tempstring += "Hearts"; 
    else if (suit == 3) 
     tempstring += "Spades"; 
    stringEnd: 
    return tempstring; 
} 

#endif 

私はそれが正しいコンパイルしていない理由はわかりません。すべてが私には大丈夫だと思われる。

+0

これは問題ではありませんが、アンダースコアの後に大文字(_CARD_H)が続く名前と、2つの連続したアンダースコアを含む名前は、実装で使用するために予約されています。あなたのコードでそれらを使用しないでください。 –

答えて

5

.cppファイルに#includeガードを使用しないでください。あなたの.hファイルは、_CARD_Hが既に定義されているため、基本的には解析されません。

0
  1. enum宣言の最後にセミコロンがありません。
  2. ので、Card.hは

    #ifndef _CARD_H 
    #define _CARD_H 
    
    #include <iostream> 
    #include <string> 
    
    using namespace std; 
    
    enum RANK{Joker, Ace = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}; 
    enum SUIT{Clubs, Diamonds, Hearts, Spades}; 
    
    class Card{ 
    private: 
        //Rank and Suit variables for all cards 
        int rank; 
        int suit; 
    public: 
        //Constructors 
        Card(); 
        Card(int r, int s); 
    
        //Getters 
        int getRank(); 
        int getSuit(); 
    
        //Setters 
        void setRank(int r); 
        void setSuit(int s); 
    
        //toString 
        string toString(); 
    }; 
    
    #endif 
    

    ようになり、Card.cppが

    ようになるの#include以外Card.cppで必要とされていない "Card.h"

が含まれています

#include "Card.h" 
//Default constructor 
Card::Card() 
{ 
    rank=Joker; 
    suit=Clubs; 
} 

//Constructor 
Card::Card(int r, int s) 
{ 
    rank = r; 
    suit = s; 
} 

//Getters for rank and suit 
int Card::getRank() 
{ 
    return rank; 
} 
int Card::getSuit() 
{ 
    return suit; 
} 

//Setters for rank and suit 
void Card::setRank(int r) 
{ 
    rank = r; 
} 
void Card::setSuit(int s) 
{ 
    suit = s; 
} 

//toString function for output 
string Card::toString() 
{ 
    string tempstring = ""; //list of if-else statements for what to add to the string that gets printed 
    if (rank == 0) 
    { 
     tempstring += "Joker"; 
     goto stringEnd; //sends the process to the end of the list if rank is Joker so it doesn't attempt to add a suit to the card toString 
    } 
    else if (rank == 1) 
     tempstring += "Ace of "; 
    else if (rank == 2) 
     tempstring += "Two of "; 
    else if (rank == 3) 
     tempstring += "Three of "; 
    else if (rank == 4) 
     tempstring += "Four of "; 
    else if (rank == 5) 
     tempstring += "Five of "; 
    else if (rank == 6) 
     tempstring += "Six of "; 
    else if (rank == 7) 
     tempstring += "Seven of "; 
    else if (rank == 8) 
     tempstring += "Eight of "; 
    else if (rank == 9) 
     tempstring += "Nine of "; 
    else if (rank == 10) 
     tempstring += "Ten of "; 
    else if (rank == 11) 
     tempstring += "Jack of "; 
    else if (rank == 12) 
     tempstring += "Queen of "; 
    else if (rank == 13) 
     tempstring += "King of "; 
    if (suit == 0) 
     tempstring += "Clubs"; 
    else if (suit == 1) 
     tempstring += "Diamonds"; 
    else if (suit == 2) 
     tempstring += "Hearts"; 
    else if (suit == 3) 
     tempstring += "Spades"; 
    stringEnd: 
    return tempstring; 
} 

これはうまくコンパイルする必要があります。

関連する問題