首页 > 解决方案 > 为什么我的 cout 语句给了我一个错误?

问题描述

所以我在这个实验室工作,我试图让用户输入游戏的名称,我希望它看起来像:

输入游戏名称 x:

其中 x 是名为 games 的数组中的位置。

当我尝试使用

cout << "Input name of game " << games[i] << ": ";

games[i] 之前的小于符号有错误,我不知道为什么。

主文件:

#include <iostream>
#include <iomanip>
#include <string>
    
    #include "game.h"
    
    using namespace std;
    
    int main()
    {
        int numGames;
        int randomNumber;
        string name;
        string genre;
    
        cout << "How many games have you played in the last year?\n";
        cin >> numGames;
    
        game* games = new game[numGames];
        srand(100);
    
        for (int i = 0; i < numGames; i++)
        {
            cout << "What is the name of the game?\n";
            cin >> name;
            cout << "What is the genre of " << name << "?\n";
            cin >> genre;
    
            cout << "Input name of game " << games[i] << ": ";
    
            game* ptrObj = new game();
            ptrObj->setName(name);
            ptrObj->setGenre(genre);
            randomNumber = rand() % 10 + 1;
            ptrObj->setDifficulty(randomNumber);
            games[i] = *ptrObj;
        }
    }

游戏.h:

#pragma once

#include <iostream>
#include <string>

class game
{
public:
    game();
    ~game();
    void setName(std::string);
    std::string getName();
    void setGenre(std::string);
    std::string getGenre();
    void setDifficulty(int);
    int getDifficulty();
private:
    std::string name;
    std::string genre;
    int difficulty;
};

游戏小鬼:

#include "game.h"

#include <iostream>
#include <string>

game::game()
{
    std::cout << "Creating a new game\n";
}

game::~game()
{
    std::cout << "In the game destructor\n";
}

void game::setName(std::string n)
{
    this->name = n;
}

std::string game::getName()
{
    return this->name;
}

void game::setGenre(std::string g)
{
    this->genre = g;
}

std::string game::getGenre()
{
    return this->genre;
}

void game::setDifficulty(int d)
{
    this->difficulty = d;
}

int game::getDifficulty()
{
    return this->difficulty;
}

标签: c++

解决方案


推荐阅读