cout不是std的成员,以及其他与c ++有关的问题 [英] cout is not a member of std, and other issues regarding c++

查看:720
本文介绍了cout不是std的成员,以及其他与c ++有关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

允许我通过说我确实包括了它(也适用于string,endl,并且从字面上看,所有内容都不起作用)作为开头.就语法而言,我的IDE并未显示任何错误;我不明白为什么会发生这个问题?在我编写的其他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;
}

我的标题:

#ifndef BULLSANDCOWS_H
#define BULLSANDCOWS_H



class BullsAndCows {

public:


    void playGame(int gameDiff);


};

#endif

最后,我的BullsAndCows.cpp文件:

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;
    }

}

我收到的错误是"cout不是std的成员",cout符号不能在using声明中使用.在此之前,我使用使用命名空间std",并且会返回诸如"BullsAndCows"之类的错误,而不是命名空间或类(如果它不是类,那么我必须是火星人).还有一些关于缺少;"的事情在userInput之前,例如在我的main.cpp代码中;这没有任何意义,因为没有什么可以缺少的.我正在使用VS2017.为什么C ++如此令人讨厌的语言可以使用?

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?

推荐答案

放置指令

#include "stdafx.h"

先于其他include指令.

before any other include directives.

这篇关于cout不是std的成员,以及其他与c ++有关的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆