简体   繁体   中英

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. I added a few Items into the Item vector and tried to make a getter for that Item Vector. 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:

.....
///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:

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. 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; use '&' to create a pointer to member 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:

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

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; use '&' to create a pointer to member 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 . 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). 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. Room::GetItems() does not produce output - it returns a std::vector<Item> . Retrieving an element from a vector, using operator[]() , obtains a reference to the corresponding Item , so also does not produce output.

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...

The code here is the same as the previous lot, except you have re-inserted the cout << in the loop. That causes the compiler to try to find and call an operator<<() function for writing an Item to an output stream. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM