Cout 不是 STD 的成员,以及有关 C++ 的其他问题
cout is not a member of std, and other issues regarding c++
请允许我先说我确实包含了(也适用于字符串,endl,而且字面上一切都不起作用); 就语法而言,我的 IDE 没有显示任何错误; 我不明白为什么会发生这个问题?它在我编写的其他C++代码示例中运行良好。
所以我正在尝试做一个小游戏,公牛和奶牛。我的主代码如下所示:
#include <iostream>
#include "stdafx.h"
#include "BullsAndCows.h"
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main()
{
string userInput = "";
bool playAgain = false;
int gameDiff;
constexpr char * GREETING = "Welcome to Bulls and Cows! Please enter the difficulty level: (1) Easy, (2) Medium, (3) Hard";
cin >> gameDiff;
do
{
BullsAndCows *bc = new BullsAndCows();
bc->playGame(gameDiff);
} while (playAgain);
constexpr char * INFORMATION = "Total Word Length is: ";
//Introduce the game.
cout << GREETING <<endl;
return 0;
}
我的标题:
#ifndef BULLSANDCOWS_H
#define BULLSANDCOWS_H
class BullsAndCows {
public:
void playGame(int gameDiff);
};
#endif
最后,我的BullsAndCows.cpp文件:
#include <iostream>
#include <string>
#include "stdafx.h"
#include "BullsAndCows.h"
using std::cout;
using std::endl;
using std::cin;
using std::string;
void BullsAndCows::playGame(int gameDiff) {
string wordGuess = "";
string secretWord = "";
int numBulls = 0;
int numCows = 0;
int numGuesses = 0;
switch (gameDiff)
{
case 1: {
numGuesses = 30;
secretWord = "Hello";
for (int i = 0; i < 30; i++) {
numBulls = 0;
numCows = 0;
cout << "Word Length to Guess Is Five, you have " << numGuesses << " guesses remaining" << endl;
cout << "Enter your word guess";
cin >> wordGuess;
for (int j = 0; j < wordGuess.length; j++) {
if (wordGuess.at(j) == secretWord.at(j)) {
numBulls++;
}
else if (secretWord.find(wordGuess.at(j)) != -1) {
numCows++;
}
}
cout << "Bulls: " << numBulls << endl;
cout << "Cows: " << numCows << endl;
if (numBulls == secretWord.length) {
cout << "YOU WIN!" << endl;
break;
}
}
break;
}
case 2:
numGuesses = 20;
break;
case 3:
numGuesses = 10;
break;
}
}
我得到的错误是"cout 不是 std 的成员",cout 符号不能在 using 声明中使用。在此之前,我使用"使用命名空间 std",这将返回诸如"BullsAndCows"不是命名空间或类之类的错误(如果它不是类,那么我一定是火星人)。还有关于用户输入之前缺少";"的东西,例如在我的主.cpp代码中;这是没有意义的,因为没有什么遗漏的。我正在使用VS2017。为什么C++这么烦人的语言
?放置指令
#include "stdafx.h"
在任何其他包含指令之前。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 在其他文件中创建类时在 c++ 项目中不起作用
- 类与私有变量的其他类之间的线程安全性
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- GlobalAlloc而不是其他分配方法
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 结构和双指针隐藏在其他结构中,多层混淆
- UE4在OnComponentBeginOverlap上铸造其他actor
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- MESI协议和std::atomic-它是否确保所有写入立即对其他线程可见?
- 保留对其他类的成员函数的引用
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 具有包含其他对象的类的对象创建顺序
- 让bool方法返回其他整数