需要c ++类的帮助 [英] need help with c++ classes

查看:87
本文介绍了需要c ++类的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何从一个函数中设置的类返回值有点困惑,需要在另一个函数中使用。以下代码是我的函数,它给了我正在寻找的值。我需要返回这些值并使用它。我无法提供我的整个代码,因为它是我正在处理的项目。



I'm a little bit confused about how to return a value from a class that is set in one function, and needs to be used in another function. the following code is my function which give me the values I'm looking for. I need to return those values and use it. I can't provide my entire code because its a project I'm working on.

int Assignedlocations(int board[][9])
{
    eightQueen row1, col1; 
    int row, col;
    for (row = 0; row < 9; row++)
    {
        for (col = 0; col < 9; col++)
        {
        if (board[row][col] == 0 )
        {
            row1.setrow(row); //need to use this value in my other function
            col1.setcol(col);// need to uuse this value in my other function
        }
    }
}     
   
class eightQueen
{

private:
    int value;

public:
    eightQueen()
    {
        value = 0;
    }

    void setrow(int x)
    {
        value =x;
    }

    void  setcol(int y)
    {
        value = y;
    }

    int getrow()
    {
        return value;
    }

    int getcol()
    {
        return value;
    }

};

推荐答案

您正在尝试使用行和列中的一个值。在值写入另一个时,如果设置行,则松散列值,反之亦然。不要使用一个字段,而是使用两个字段: row ,单独分配和读取它们。更好的是,使用两个字段引入名为 Location struct 。您不需要 int 值,行和列可以存储在一个字节中。一件重要的事情:这个数字类型应该是无符号的:位置不能是负数。



我不知道怎么回事。你的问题不是C ++,而只是基本思维,这对教学来说几乎没用。我回答了这个问题,但是我的这些指示不能教导思考。如果其他情况有所不同,但就像这个一样简单,你需要再次寻求帮助吗?也许你应该永远记住你是一个人。



-SA
You are trying to use one value from row and column. On value writes over another, so you loose column value if you set row and visa versa. Use not one field, but two: row and column, assign and read them separately. Better yet, introduce the struct called Location with two fields. You don't need int values, the row and column can be stored in a byte. One important thing: this numeric type should be unsigned: the positions cannot be negative.

I don't know how else to help. Your problem is not C++ but just elementary thinking, which is nearly useless to teach. I answered the question, but such instructions as mine cannot teach thinking. If some other situation is different but as simple as this one, would you need to ask for help again? Perhaps you should just always remember that you are a human being.

—SA


嗯,如果你想一想就很明显了:你需要两个不同的 - 一个用于行,一个用于列:

Well, it's fairly obvious if you think about it: you need two different values of value - one for row and one for column:
private:
    int rowValue;
    int colValue;

public:
    eightQueen()
    {
    rowValue = 0;
    colValue = 0;
    }

    void setrow(int x)
    {
        rowValue =x;
    }

    void  setcol(int y)
    {
        colValue = y;
    }

    int getrow()
    {
        return rowValue;
    }

    int getcol()
    {
        return colValue;
    }


这篇关于需要c ++类的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆