另一个未在此范围内声明的'声明时发出问题

时间:2017-09-27 02:45:18

标签: c++ scoping

我目前正在为一个班级的卡片程序工作,我遇到了一个问题,编译器在这个问题上告诉我,当它们存在并且没有声明某些东西时,事物没有在范围内声明什么时候呢。这是代码:

Card.h:

Write-Host "##vso[task.setvariable variable=PATH;]${env:PATH};C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin";

Card.cpp:

#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

我不确定为什么它没有正确编译。一切似乎都没问题。

2 个答案:

答案 0 :(得分:5)

您不应在#include文件中使用.cpp警卫。 <{1}}文件基本上没有被解析,因为已经定义了.h

答案 1 :(得分:0)

  1. 枚举声明结束时缺少分号。
  2. 除了#include“Card.h”
  3. 之外,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;
    }
    

    它应该编译好。