Meilianto Luffenz
Meilianto Luffenz

Reputation: 99

c++ pass an object into method from another class

I have class A and method call a_method, I have class B with and object b_object, and I wanted to pass object b_object as an argument into a_method, but how do I declare it in a_method?

#include <iostream>
#include <vector>

class Board{
    int sizeofboard;
    std::vector<std::vector<int>> board;
public:
    Board(int sizeofboard){
        this->sizeofboard = sizeofboard;
        int n = 1;
        for (int i = 0; i < sizeofboard; i++){
            board.push_back(std::vector<int>());
            for (int j = 0; j < sizeofboard; j++){
                board[i].push_back(n);
                n++;
            }
        }
    }
    display(){
        for (int i = sizeofboard-1; i >= 0; i--){
            std::cout << " ";
            for (int j = 0; j < sizeofboard; j++){
                if (j == sizeofboard-1){
                    std::cout << board[i][j];
                    continue;
                }
                std::cout << board[i][j] << " | ";
            }
            std::cout << std::endl;
            if (i == 0)continue;
            std::cout << "---+---+---" << std::endl;
        }
    }
    draw(int position, Player &player){
        int i, j;
        int i = position/3;
        if (i == 0)i = 2;
        j = position%3;
    }
};

class Player{
    char symbol;
    public:
};

int main()
{
    Board board(3);
    Player player1;
    board.display();
    return 0;
}

so the part of draw I am confused on how to declare it. it says Player has not been declared

Upvotes: 1

Views: 1295

Answers (2)

Alan
Alan

Reputation: 1

The below working example shows what you want:

#include <iostream>
#include <vector>
class Player{
    public:
        char symbol = 'A';
    
};
class Board{
    int sizeofboard;
    std::vector<std::vector<int>> board;
public:
    Board(int sizeofboard){
        this->sizeofboard = sizeofboard;
        int n = 1;
        for (int i = 0; i < sizeofboard; i++){
            board.push_back(std::vector<int>());
            for (int j = 0; j < sizeofboard; j++){
                board[i].push_back(n);
                n++;
            }
        }
    }
    //this display function takes Player be reference
    void display(Player &player){
        for (int i = sizeofboard-1; i >= 0; i--){
            std::cout << " ";
            for (int j = 0; j < sizeofboard; j++){
                if (j == sizeofboard-1){
                    std::cout << board[i][j];
                    continue;
                }
                std::cout << board[i][j] << " | ";
            }
            std::cout << std::endl;
            if (i == 0)continue;
            std::cout << "---+---+---" << std::endl;
        }
     std::cout<<player.symbol<<std::endl;//this prints the player1 symbol value
    }
    void draw(int position, Player &player){
        int  j;
        int i = position/3;
        if (i == 0)i = 2;
        j = position%3;
    }
};


int main()
{
    Board board(3);
    Player player1;
    board.display(player1);//pass player1 object by reference. 
    return 0;
}

Note that from inside the display() method we have printed out the symbol data member's value.

Upvotes: 1

Sam Varshavchik
Sam Varshavchik

Reputation: 118435

    draw(int position, Player &player){

A C++ compiler compiles a C++ program from beginning to the end. At this point, your C++ compiler does not know anything about this mysterious class called Player, which has not been declared yet.

You can simply move the definition of Player before this class. In other cases a forward declaration is all that's needed.

In situations involving complex dependencies between classes it might be necessary to merely declare all classes, first, then defer the definition of all class members until everything is fully defined.

In this case moving the declaration first will solve this particular error (but not other, unrelated compilation errors). This may change, depending on any code that's not shown here.

Upvotes: 2

Related Questions