我做了一个名为“Item”的类和一个名为“Room”的类,并且有一个用于Item类型的矢量房间内的“物品”。 我在物品矢量中添加了一些物品,并尝试为该物品矢量制作一个吸气剂。现在我试图打印吸气剂,看看它是否真的得到了我想要的,但是当我尝试一种方法时,它给了我一个错误信息,或者当我尝试不同的方式时,什么也不打印。我究竟做错了什么?我想检查一个类矢量的吸气工作是通过打印工作,但它不会工作
Room.h有一些东西,以及这些代码:
.....
///Getters
//get a list of the items currently in the room
vector<Item> GetItems();
private:
///properties
string RoomName;
string RoomDescription;
vector <Door> Doors;
vector <Item> Items;
Room.cpp有事情,定义的默认和重载的房间和房间了一些项目,也有这些:
vector<Item>Room::GetItems()
{
return Items;
}
int Room::GetItemAmount()
{
return Items.size();
}
main.cpp有一些push.backs和东西,它似乎是正确包含在向量中的项目。现在,我不知道如何打印消气吧吧...尝试此:
Room FunStoneRoom = Room();
FunStoneRoom.AddItem(ItemCharcoal);
for (unsigned int VectorPos = 0; VectorPos < FunStoneRoom.GetItemAmount(); VectorPos++)
{
cout << FunStoneRoom.GetItems[VectorPos] << " ";
}
cout << endl;
这给了我一个错误: 严重性代码说明项目文件的线路抑制状态 错误C3867“客房:: GetItems ':非标准语法;使用 '&' 创建一个指针构件ConsoleApplication25 d:\ tiltan \视觉工作室\ ownclasses \室温+ +项门\ consoleapplication25 \ main.cpp中51
我也尝试:
for (unsigned int VectorPos = 0; VectorPos < FunStoneRoom.GetItemAmount(); VectorPos++)
{
FunStoneRoom.GetItems()[VectorPos];
}
cout << endl;
其没有按给出一个错误,但只是打印一个空行。 和:
for (unsigned int VectorPos = 0; VectorPos < FunStoneRoom.GetItemAmount(); VectorPos++)
{
cout << FunStoneRoom.GetItems()[VectorPos];
}
cout << endl;
这标志着我< <有一个红色的线,并告诉我没有操作员“< <”这些操作数相匹配... 我怎么去呢? 我真的没那么先进,不知道很多复杂的功能和代码,所以请尽量简单,因为你可以跟我在一起。 我在这里也很新很抱歉,如果我没有发布或正确解释自己...
编辑:每请求 - 我添加item.h和item.cpp,但记住我不需要知道它们包含的内容,只是在载体的项目清单: item.h:
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Item
{
public:
///constructors
Item(); //default item
///overloadeds
//overloaded customizable items
// @param string = Item Name
// @param string = Item Description
Item(string, string);
///destructors
~Item();
///methods
//Display item name and description
void ViewItem();
//Set a new Item Description
void SetItemDescription(string);
//Set a new Item Name
void SetItemName(string);
//Get an Item's name
string GetItemName();
//Get an Item's description
string GetItemDescription();
private:
///properties
string ItemName;
string ItemDescription;
};
item.cpp:
#include "Item.h"
Item::Item()
{
Item::ItemName = "Non Material Item";
Item::ItemDescription = "You cannot see, feel, taste, smell or hear this item";
}
Item::Item(string NewItemName, string NewItemDesc)
{
NewItemName[0] = toupper(NewItemName[0]);
Item::ItemName = NewItemName;
Item::ItemDescription = NewItemDesc;
}
Item::~Item()
{
}
void Item::ViewItem()
{
cout << ItemName << endl;
cout << ItemDescription << endl;
}
void Item::SetItemDescription(string NewItemDescription)
{
if (NewItemDescription.length() < 100)
{
NewItemDescription[0] = toupper(NewItemDescription[0]);
ItemDescription = NewItemDescription;
}
else
{
ItemDescription = "This Item's description is too long";
}
}
void Item::SetItemName(string NewItemName)
{
if (NewItemName.length() < 30)
{
NewItemName[0] = toupper(NewItemName[0]);
ItemName = NewItemName;
}
else
{
ItemDescription = "This Item's name is too long";
}
}
string Item::GetItemName()
{
return ItemName;
}
string Item::GetItemDescription()
{
return ItemDescription;
}
for this cout << FunStoneRoom.GetItems()[VectorPos];为了工作,你需要重写项目类 – user1438832
的<<操作符不确定这是什么意思.. –
请告诉我们如何定义项目。 –