简体   繁体   English

我正在尝试通过打印它来检查类向量的getter是否正常工作但是它不起作用

[英]I'm trying to check if a getter for a class vector is working by printing it but it won't work

I made a class called "Item" and a class called "Room" and there's a vector for Item types called "Items" inside Room. 我创建了一个名为“Item”的类和一个名为“Room”的类,在Room中有一个名为“Items”的Item类型的向量。 I added a few Items into the Item vector and tried to make a getter for that Item Vector. 我在项目向量中添加了一些项目,并尝试为该项目向量创建一个getter。 Now i'm trying to print the getter to see if it really did get what i wanted but it gives me an error message when i try one way or just prints nothing when i try a different way. 现在我正在尝试打印吸气剂,看它是否确实得到了我想要的但是当我尝试一种方式时它给我一个错误信息或者当我尝试不同的方式时只打印任何东西。 What am i doing wrong? 我究竟做错了什么?

Room.h has some stuff as well as these lines of code: 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 has things that defined default and overloaded rooms and gave rooms some items and also has these: Room.cpp有定义默认和超载房间的东西,并为房间提供了一些物品,还有以下内容:

vector<Item>Room::GetItems()
{
    return Items;
}

int Room::GetItemAmount()
{
    return Items.size();
}

main.cpp has some push.backs and stuff and it appears that the items are properly contained in the vector. main.cpp有一些push.backs和东西,看起来这些项正确包含在向量中。 Now i'm not sure how to print the getter for it it... trying this: 现在我不知道如何打印它的吸气剂...尝试这个:

Room FunStoneRoom = Room();

    FunStoneRoom.AddItem(ItemCharcoal);

        for (unsigned int VectorPos = 0; VectorPos < FunStoneRoom.GetItemAmount(); VectorPos++)
        {
            cout << FunStoneRoom.GetItems[VectorPos] << " ";
        }

    cout << endl;

This gives me an error : Severity Code Description Project File Line Suppression State Error C3867 'Room::GetItems': non-standard syntax; 这给了我一个错误:严重性代码描述项目文件行抑制状态错误C3867'Room :: GetItems':非标准语法; use '&' to create a pointer to member ConsoleApplication25 d:\\tiltan\\visual studio\\ownclasses\\room+item+door\\consoleapplication25\\main.cpp 51 使用'&'创建指向成员的指针ConsoleApplication25 d:\\ tiltan \\ visual studio \\ ownclasses \\ room + item + door \\ consoleapplication25 \\ main.cpp 51

I also tried: 我也尝试过:

for (unsigned int VectorPos = 0; VectorPos < FunStoneRoom.GetItemAmount(); VectorPos++)
        {
            FunStoneRoom.GetItems()[VectorPos];
        }

    cout << endl;

which doesn't give an error but just prints an empty line. 这不会给出错误但只打印一个空行。 and: 和:

for (unsigned int VectorPos = 0; VectorPos < FunStoneRoom.GetItemAmount(); VectorPos++)
        {
            cout << FunStoneRoom.GetItems()[VectorPos];
        }

    cout << endl;

which marks my << with a red line and tells me no operator "<<" matches these operands... How do i go about this? 这标志着我的<<用红线告诉我没有操作符“<<”匹配这些操作数......我怎么去做这个? I'm really not that advanced and don't know a lot of complicated functions and codes and whatnot so please try to be as simple as u can with me. 我真的不是那么先进,也不知道很多复杂的功能和代码,所以请尽量让我和你一样简单。 i'm also new here so sorry if i'm not posting or explaining myself properly... 我也是新来的,很抱歉,如果我没有正确地发布或解释自己......

EDIT: per requested - i'm adding item.h and item.cpp but remember i don't need to know what they contain, only a list of the items in the vector: item.h: 编辑:按要求 - 我正在添加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: 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;
}

A sample example is - 一个示例是 -

#include <iostream>  
using namespace std;  

class Item  
{  
    int id;
    string description;
public:  
    Item(int id,string description)  
    {  
        this->id=id;
        this->description=description;
    }  
    friend ostream& operator<<(ostream& os, const Item& it);  
};  

ostream& operator<<(ostream& os, const Item& it)  
{  
    os << "Id : " << it.id << " Description : " <<it.description<<endl;  
    return os;  
}  

int main()  
{  
    Item it(5, " calculator");  
    cout << it;  
} 

With your first concern, which I quote here .... 关于你的第一个问题,我在这里引用....

 Room FunStoneRoom = Room(); FunStoneRoom.AddItem(ItemCharcoal); for (unsigned int VectorPos = 0; VectorPos < FunStoneRoom.GetItemAmount(); VectorPos++) { cout << FunStoneRoom.GetItems[VectorPos] << " "; } cout << endl; 

This gives me an error : Severity Code Description Project File Line Suppression State Error C3867 'Room::GetItems': non-standard syntax; 这给了我一个错误:严重性代码描述项目文件行抑制状态错误C3867'Room :: GetItems':非标准语法; use '&' to create a pointer to member ConsoleApplication25 d:\\tiltan\\visual studio\\ownclasses\\room+item+door\\consoleapplication25\\main.cpp 51 使用'&'创建指向成员的指针ConsoleApplication25 d:\\ tiltan \\ visual studio \\ ownclasses \\ room + item + door \\ consoleapplication25 \\ main.cpp 51

In this case, the cause is a missing () on the call of GetItems . 在这种情况下,原因是GetItems调用时缺少() The compiler is treating this as an attempt to get a pointer to the member function Room::GetItems (since similar syntax, when applied to non-member functions, converts the name of a function into a pointer to that function). 编译器将此视为尝试获取指向成员函数Room::GetItems的指针(因为类似的语法,当应用于非成员函数时,将函数的名称转换为指向该函数的指针)。 That is an error, since pointers to member function are not obtained that way. 这是一个错误,因为指向成员函数的指针不是那样获得的。

With your second concern, which I quote here 关于你的第二个问题,我在这里引用

I also tried: 我也尝试过:

 for (unsigned int VectorPos = 0; VectorPos < FunStoneRoom.GetItemAmount(); VectorPos++) { FunStoneRoom.GetItems()[VectorPos]; } cout << endl; 

which doesn't give an error but just prints an empty line. 这不会给出错误但只打印一个空行。

In this case, the behaviour you see is correct. 在这种情况下,您看到的行为是正确的。 In the loop, you have removed the cout << , but are still surprised that it is producing no output. 在循环中,你已经删除了cout << ,但仍然感到惊讶的是它没有产生输出。 Room::GetItems() does not produce output - it returns a std::vector<Item> . Room::GetItems()不产生输出 - 它返回一个std::vector<Item> Retrieving an element from a vector, using operator[]() , obtains a reference to the corresponding Item , so also does not produce output. 使用operator[]()从向量中检索元素,获取对相应Item的引用,因此也不会生成输出。

and: 和:

 for (unsigned int VectorPos = 0; VectorPos < FunStoneRoom.GetItemAmount(); VectorPos++) { cout << FunStoneRoom.GetItems()[VectorPos]; } 

cout << endl; cout << endl;

which marks my << with a red line and tells me no operator "<<" matches these > operands... 这标志着我的<<用红线告诉我没有操作符“<<”匹配这些>操作数......

The code here is the same as the previous lot, except you have re-inserted the cout << in the loop. 这里的代码与前一批相同,只是你已经在循环中重新插入了cout << That causes the compiler to try to find and call an operator<<() function for writing an Item to an output stream. 这导致编译器尝试查找并调用operator<<()函数以将Item写入输出流。 Your code does not declare such a function, so the compiler cannot find it. 您的代码未声明此类函数,因此编译器无法找到它。

You need to create such a function. 您需要创建这样的功能。 It needs to be declared in item.h and defined (implemented) in item.cpp . 它需要被宣布item.h和定义(实现) item.cpp Those files contain no such declarations. 这些文件不包含此类声明。

Now we come to your plea for help (which I've already given you). 现在我们来请求你的帮助(我已经给你了)。

How do i go about this? 我该怎么做? I'm really not that advanced and don't know a lot of complicated functions and codes and whatnot so please try to be as simple as u can with me. 我真的不是那么先进,也不知道很多复杂的功能和代码,所以请尽量让我和你一样简单。

I'm going to be blunt. 我会直言不讳。 You are the cause of your own problems here. 你在这里是你自己问题的原因。

Firstly, you are not putting enough effort into interpreting what the compiler is trying to tell you. 首先,您没有花足够的精力来解释编译器试图告诉您的内容。 Compilers are ignorant pieces of software but, in their ignorance, they complain fairly consistently about particular types of error. 编译器是无知的软件,但由于他们的无知,他们对特定类型的错误抱怨相当一致。 So you need to apply effort into understanding your compiler's error messages. 因此,您需要努力了解编译器的错误消息。 If you don't, you'll never be able to fix your own code when a compiler rejects it. 如果不这样做,当编译器拒绝它时,您将永远无法修复自己的代码。

Then you are compounding your problems by randomly changing code in the hope of fixing it, rather than making reasoned changes. 然后,您通过随机更改代码来复合您的问题,希望修复它,而不是进行合理的更改。 The result of that is either behaviour your don't understand (if the code compiles and runs) or error messages from your compiler that you won't understand, unless you understand the change you actually made. 结果是您不理解的行为(如果代码编译并运行)或编译器中您不理解的错误消息,除非您了解实际所做的更改。

When you are dealing with a confirmed ignoramus (the compiler) you cannot afford to be ignorant. 当你处理一个确认的无知(编译器)时,你不能无知。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试编写一个 class,其中子 class 将继承父 class 的方法,但我的代码不会编译 - I'm trying to write a class where the child class will inherit the methods from the parent class, but my code won't compile 我正在尝试从 GMocked class 返回 rapidjson::Value 但我似乎无法让它工作 - I'm trying to return a rapidjson::Value from a GMocked class but I can't seem to get it to work std :: cin &gt;&gt; vector [i]不能让迭代器正常工作吗? - std::cin >> vector[i] won't let iterators work properly? 我正在尝试使用stoi(),但是编译器正在尝试给我替换品,并且不会编译 - I'm trying to use stoi(), but the compiler is trying to give me replacements and won't compile 我正在尝试为我的 DynamicArray 类创建一个迭代器。 为什么 STL 排序不适用于我的迭代器? - I'm trying to create an iterator for my DynamicArray class. Why doesn't STL sort work with my iterator? 向量排序和擦除将不起作用 - vector sort and erase won't work 我正在尝试读取 class 指向带有 for 循环的向量的指针,但它仅在我在循环内声明 object 时才有效 - I'm trying to read in class pointers to a vector with a for loop, but it only works if I declare object inside loop 我正在使用模数来处理密码,字母“ u”无法正确解码 - I'm working on a cipher code using modulos and the letter “u” won't decode properly 打印我的矢量不起作用(没有编译器错误) - Printing My Vector Doesn't Work (No Compiler Errors) 我在多维数组输入和 output 中苦苦挣扎,我管理输入,但是打印无法按计划进行 - I'm struggeling with Multi-Dimension array input and output, I managed input, however printing doesn't work as planned
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM