c++ - 为什么我的 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;
}
解决方案
推荐阅读
- azure-devops - STDIN 在列出 Azure VM 时被列出,因此出现“资源 ID 无效”错误
- python - Matplotlib为什么颜色条y标签会随着多个颜色条消失
- angular - NbDialog 上的 ngModel 是只读的?
- macos - 在 Mac OSX 上构建的 Docker 映像无法在 AWS EC2 实例上运行
- php - PHP + Mysql:如何提取按相似度排序的相似结果
- gradle - 无法加载类 grails.plugins.rendering.image.ImageRenderingService Grails 4 渲染:2.03
- python - 以节省时间和内存的方式使用多处理
- clickhouse - ClickHouse Kafka 表引擎和仅一次支持
- javascript - 在Javascript中按属性值排序对象时如何解决Typescript“对象可能未定义”警告
- c++ - Wint-in-bool-context 是什么导致了这个警告?