如何定义返回类型类的函数?

时间:2014-10-22 18:27:11

标签: c++ function cygwin return-type

所以我正在为类创建一个程序,我有一个名为“Item”的头文件,我在我的主文件中有一个名为“Room”的类,我正在尝试创建一个返回类型Item和Cygwin的方法给我页面和页面的错误!

到目前为止,这是我的代码:

项目标题文件:

class Item
{
    std::string description;

    public: Item (std::string newDescription)
    {
        description = newDescription;
    }

    public: std::string getDescription()
    {
        return description;
    }
};

这是主要的课程:

        #include <iostream>
    #include <string>
    #include "Item.h"

    class Room
    {
        std::string description;
        int exits[2][4];
        std::string items[10];

        Room(std::string description)
        {
            this -> description = description;
        }

        void setExit (int direction, int neighbor) //Direction - 1=N, 2=E, 3=S, 4-W
        {
            for (int i = 0; i < 4; i++)
            {
                if (exits[0][i] == NULL)
                {
                    exits[0][i] = neighbor;
                    exits[1][i] = direction;
                    break;
                }
            }
        }

        std::string getShortDescription()
        {
            return description;
        }

        std::string getLongDescription()
        {
            return "You are " + description + ".\n" + getExitString();
        }

        std::string getExitString()
        {
            std::string returnString = "Exits:";

            for (int i = 0; i < 4; i++)
            {
                if (exits[1][i] != NULL)
                {
                    std::string tempDirection;

                    switch(exits[1][i])
                    {
                        case 1: tempDirection = "North";
                                break;

                        case 2: tempDirection = "East";
                                break;

                        case 3: tempDirection = "South";
                                break;

                        case 4: tempDirection = "West";
                                break;
                    }

                    returnString += " " + tempDirection;
                }

                else
                {
                    break;
                }
            }

            returnString += "\nItems in the room:\n";
            //returnString += getRoomItems();
            return returnString;
        }

        /*Item getItem(std::string itemName)
        {
            int size = 0;
            for (int i = 0; i < 10; i++)
            {
                if (items[i] == NULL)
                {
                    break;
                }

                else
                {
                    size++;
                }   
            }

            for (int i = 0; i < size; i++)
            {
                if (items[i] == itemName)
                {
                    return items[i];
                }
            }
        }*/

        int getExit(int direction)
        {
            for (int i = 0; i < 4; i++)
            {
                if (exits[1][i] == direction)
                {
                    return exits[0][i];
                }
            }
        }
    };

    using namespace std;

    int main()
    {

    }

将错误放在这里需要永远,所以我会跳过它抱歉!

任何帮助都会非常有帮助!

1 个答案:

答案 0 :(得分:2)

您建议实施此方法有几个问题:

Item getItem(std::string itemName)
{
    int size = 0;
    for (int i = 0; i < 10; i++)
    {
        // items[i] is of type std::string, which is a value type, not a pointer. It
        // makes no sense to compare it to null, because it can't even be null.
        if (items[i] == NULL)
        {
            break;
        }

        else
        {
            size++;
        }   
    }

    for (int i = 0; i < size; i++)
    {
        if (items[i] == itemName)
        {
            // Here you return an std::string, not an Item -- however, Item contains
            // a one-argument constructor accepting std::string and is not marked
            // "explicit", so this line is equivalent to "return Item(items[i]);".
            // This may or may not be what you intended, but would not cause a
            // compile-time error.
            return items[i];
        }
    }

    // You do not return anything if program flow makes it to this point. This
    // causes an undefined value to be returned, and you don't want that. You need
    // to return something here -- but it can't be null, because Item is a value
    // type!
}

请考虑使用std::vector<std::string>作为items的类型。向量是一个可以自动增长的可变长度容器。 (想想Java的List<>。)或者,您可能希望项目名称映射到Item个实例,在这种情况下,您可以使用std::map<std::string, Item>

相关问题