宣布时的另一个'not declared in this scope'问题
Yet another 'not declared in this scope' issue when it is declared
我目前正在为班级的卡程序和遇到一个问题,其中编译器告诉我,当它们在范围内没有在范围内声明,有些事当它是完全没有声明的。这是代码:
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
我不确定为什么它不正确。一切对我来说似乎都还好。
您不应在.cpp
文件中使用#include
警卫。您的.h
文件基本上根本没有被解析,因为_CARD_H
已经定义。
- 在枚举声明结束时丢失了分号。
- card.cpp中不需要包含包含在#include" card.h" 之外
所以,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;
}
它应该填充罚款。
相关文章:
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 如何解决"cout was not declared in this scope"错误?
- 为什么程序显示我"was not declared in this scope"?
- 为什么我会收到此警告? "Member 'x' was not initialized in this constructor"
- 即使包含 <time.h 后仍"‘CLOCK_PER_SEC’ was not declared in this scope"错误>
- С 错误"s1, s2 are used uninitialised in this function"
- "C4649: attributes are ignored in this context"的含义是什么?
- 我正在接受"Error: string was not declared in this scope"
- C++ 我一直在"was not declared in this scope error"
- 将 .ui 文件导入 Qt5 C++时出错"was not declared in this scope"
- 编译 CPP 应用程序时出错。 "error: 'posix_memalign' was not declared in this scope"
- main.cpp中的错误:函数库中的函数"not declared in this scope",但实际上是
- "Error: 'dungeon_t' was not declared in this scope"但包含在标题中
- "pointer was not declared in this scope"是什么意思
- "error: not declared in this scope?"是什么意思
- 宣布时的另一个'not declared in this scope'问题
- " 'X' not declared in this scope "错误
- 子类模板错误 (C++): "error: 'root' was not declared in this scope"
- Eclipse C++ Linux "to_string was not declared in this scope"