c++ - cout不是std的成员,其他关于c++的问题

标签 c++ c++11 namespaces

请允许我先说明一下,我已经包含了(也适用于字符串、endl,而且实际上一切都不起作用);就语法而言,我的 IDE 没有显示任何错误;我不明白为什么会发生这个问题?它在我编写的其他 C++ 代码示例中运行良好。

所以我在尝试做一个小游戏,bulls and cows。我的主要代码如下所示:

#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 声明中。在此之前,我使用“使用命名空间标准”并且会返回错误,例如“BullsAndCows”不是命名空间或类(如果它不是类,那么我一定是火星人)。还有一些关于缺少“;”的事情例如在我的 main.cpp 代码中的 userInput 之前;这是没有意义的,因为没有什么遗漏的。我正在使用 VS2017。为什么 C++ 是一种令人讨厌的语言?

最佳答案

放置指令

#include "stdafx.h"

在任何其他包含指令之前。

关于c++ - cout不是std的成员,其他关于c++的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43904130/

相关文章:

c# - (C#) 使用 system.Collections.generic 时找不到类型或命名空间列表

c++ - 在这种情况下,为什么 g++ 和 clang 会破坏 namespace 抽象?

c++ - 在 C++ 中在 Windows 中播放样本缓冲区的最常见方法是什么?

c++ - 为什么add函数在c++ 11线程中没有效果?

c++ - Code::Blocks 无法识别 std::thread

asp.net - "using MyNameSpace;"和 "namespace MyNameSpace"之间的区别

c++ - 如何在stringstream的位置写

c++ - 基本类型数组的迭代器类型

c++ - 带 alpha channel 的纹理覆盖背景对象

c++ - 这个 RAII 独占资源检查对象/管理器组合线程安全吗?