Cout is not a member of std, and other issues regarding c++
Allow me to preface this by saying that I do have included (also applies to string, endl, and quite literally everything doesn't work); my IDE is showing no errors as far as syntax goes; and I cannot understand why this issue is happening? It works fine in one of my other C++ code samples I wrote.
So I am trying to make a small game, bulls and cows. My main code looks as follows:
#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;
}
My header:
#ifndef BULLSANDCOWS_H
#define BULLSANDCOWS_H
class BullsAndCows {
public:
void playGame(int gameDiff);
};
#endif
Finally, my BullsAndCows.cpp file:
#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;
}
}
Errors I am getting as "cout is not a member of std", cout symbol cannot be used in a using declaration. Before that I was using "using namespace std" and that would return errors like "BullsAndCows" is not a namespace or class (if it is not a class, then I must be a martian). Also something about a missing ";" before userInput for example in my main.cpp code; which makes no sense as there is nothing that is missing that. I am using VS2017. Why is C++ such an annoying language to work in?
Place the directive
#include "stdafx.h"
before any other include directives.
链接地址: http://www.djcxy.com/p/73084.html上一篇: C ++无法调用std :: endl