2017-09-27 14 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)가 오는 이름과 두 개의 연속 된 밑줄이있는 이름은 구현에서 사용하기 위해 예약되어 있습니다. 코드에서 사용하지 마십시오. –

답변

5

.cpp 파일에 #include 가드를 사용하지 마십시오. .h 파일은 기본적으로 _CARD_H이 이미 정의되어 있기 때문에 기본적으로 파싱되지 않습니다.

0
  1. enum 선언 끝에 세미콜론이 누락되었습니다.

그래서 사용법 #include "Card.h"를 제외하고 Card.cpp에 필요하지 않은 포함 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.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; 
} 

잘 컴파일해야합니다.