Click here to Skip to main content
15,891,718 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C++
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;
    }

};
Posted
Updated 2-Dec-13 5:01am
v2
Comments
Manfred Rudolf Bihy 2-Dec-13 11:00am    
Yeah, sure a project! Hmmm, me thinks the class name eightQueen is a dead giveaway! :P
*cough*
Sergey Alexandrovich Kryukov 2-Dec-13 11:05am    
If this is the 8-queen problem, pretty difficult one, I cannot imagine much progress if a person have such difficulties with so trivial issues. I guess OP should just start to think, which is hard to help. Please see my answer.
—SA
Manfred Rudolf Bihy 2-Dec-13 11:04am    
I just saw that all of your four questions posted so far revolve around solving the Eight Queens puzzle.
When is your assignment due?
Any progress so far except non sensical code?
Sergey Alexandrovich Kryukov 2-Dec-13 11:08am    
Good point; I did not pay attention for that. The "problem" shown in this question suggests that all previous questions passed without any use at all... I really don't know how to help it. One good think is that OP is not asking for a complete solution...
—SA
Alexander24 2-Dec-13 11:09am    
My project is already done, This is a secondary way that my teacher mentioned during class. I'm just trying to figure out how to do it for personal knowledge.

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
 
Share this answer
 
Well, it's fairly obvious if you think about it: you need two different values of value - one for row and one for column:
C#
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;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900