cout is not a member of std, and other issues regarding c++
请允许我在前言中说我确实已经包含(也适用于字符串,endl,而且字面上一切都不起作用);就语法而言,我的IDE没有显示任何错误;我无法理解为什么会出现这个问题?它在我写的其他一些C ++代码示例中工作正常。
所以我想做一个小游戏,公牛和奶牛。我的主要代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #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; } |
我的标题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #ifndef BULLSANDCOWS_H #define BULLSANDCOWS_H class BullsAndCows { public: void playGame(int gameDiff); }; #endif |
最后,我的BullsAndCows.cpp文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #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"不是命名空间或类(如果它不是类,那么我必须是火星人)。还有一些关于失踪的";"在userInput之前,例如在我的main.cpp代码中;没有任何意义,因为没有任何东西可以忽略。我正在使用VS2017。为什么C ++会成为一种讨厌的语言?
放置指令
1 | #include"stdafx.h" |
在任何其他包含指令之前。