簡體   English   中英

我正在嘗試通過打印它來檢查類向量的getter是否正常工作但是它不起作用

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

我創建了一個名為“Item”的類和一個名為“Room”的類,在Room中有一個名為“Items”的Item類型的向量。 我在項目向量中添加了一些項目,並嘗試為該項目向量創建一個getter。 現在我正在嘗試打印吸氣劑,看它是否確實得到了我想要的但是當我嘗試一種方式時它給我一個錯誤信息或者當我嘗試不同的方式時只打印任何東西。 我究竟做錯了什么?

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'Room :: GetItems':非標准語法; 使用'&'創建指向成員的指針ConsoleApplication25 d:\\ tiltan \\ visual studio \\ ownclasses \\ room + item + door \\ 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;
}

一個示例是 -

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

關於你的第一個問題,我在這里引用....

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

這給了我一個錯誤:嚴重性代碼描述項目文件行抑制狀態錯誤C3867'Room :: GetItems':非標准語法; 使用'&'創建指向成員的指針ConsoleApplication25 d:\\ tiltan \\ visual studio \\ ownclasses \\ room + item + door \\ consoleapplication25 \\ main.cpp 51

在這種情況下,原因是GetItems調用時缺少() 編譯器將此視為嘗試獲取指向成員函數Room::GetItems的指針(因為類似的語法,當應用於非成員函數時,將函數的名稱轉換為指向該函數的指針)。 這是一個錯誤,因為指向成員函數的指針不是那樣獲得的。

關於你的第二個問題,我在這里引用

我也嘗試過:

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

這不會給出錯誤但只打印一個空行。

在這種情況下,您看到的行為是正確的。 在循環中,你已經刪除了cout << ,但仍然感到驚訝的是它沒有產生輸出。 Room::GetItems()不產生輸出 - 它返回一個std::vector<Item> 使用operator[]()從向量中檢索元素,獲取對相應Item的引用,因此也不會生成輸出。

和:

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

cout << endl;

這標志着我的<<用紅線告訴我沒有操作符“<<”匹配這些>操作數......

這里的代碼與前一批相同,只是你已經在循環中重新插入了cout << 這導致編譯器嘗試查找並調用operator<<()函數以將Item寫入輸出流。 您的代碼未聲明此類函數,因此編譯器無法找到它。

您需要創建這樣的功能。 它需要被宣布item.h和定義(實現) item.cpp 這些文件不包含此類聲明。

現在我們來請求你的幫助(我已經給你了)。

我該怎么做? 我真的不是那么先進,也不知道很多復雜的功能和代碼,所以請盡量讓我和你一樣簡單。

我會直言不諱。 你在這里是你自己問題的原因。

首先,您沒有花足夠的精力來解釋編譯器試圖告訴您的內容。 編譯器是無知的軟件,但由於他們的無知,他們對特定類型的錯誤抱怨相當一致。 因此,您需要努力了解編譯器的錯誤消息。 如果不這樣做,當編譯器拒絕它時,您將永遠無法修復自己的代碼。

然后,您通過隨機更改代碼來復合您的問題,希望修復它,而不是進行合理的更改。 結果是您不理解的行為(如果代碼編譯並運行)或編譯器中您不理解的錯誤消息,除非您了解實際所做的更改。

當你處理一個確認的無知(編譯器)時,你不能無知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM