0

I want to create a global pointer so that all the functions can access the reference.

I've tried assigning the address of reference to a pointer but when I try to access the values getting compilation error.

vector<vector<char>>* table;

class Solution {
public:
    void solveSudoku(vector<vector<char>>& board) {
        table = &board;
        cout << board[0][0] << "\n" << table[0][0];
    }
};
Line 70: Char 37: error: invalid operands to binary expression ('basic_ostream<char, std::char_traits<char>>' and '__gnu_cxx::__alloc_traits<std::allocator<std::vector<char, std::allocator<char>>>, std::vector<char, std::allocator<char>>>::value_type' (aka 'std::vector<char, std::allocator<char>>'))
        cout << board[0][0] << "\n" << table[0][0];
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/cstddef:130:5: note: candidate function template not viable: no known conversion from 'basic_ostream<char, std::char_traits<char>>' to 'std::byte' for 1st argument
    operator<<(byte __b, _IntegerType __shift) noexcept
    ^
10
  • 2
    You should dereference the pointer as (*table)[0][0]. Commented May 4, 2021 at 0:22
  • 1
    @Sarthak This is the same as any other pointer. The first [0] tells the compiler "table points to an array of vector<vector<char>> and I want the first one". Think of how int x[10]; int * ptr = x; ptr[4] = 5; works. Commented May 4, 2021 at 0:26
  • 2
    @Sarthak Because operator[] is supposed to be applied on the vector, not the pointer to vector. Commented May 4, 2021 at 0:26
  • 3
    Why is there a need to create a pointer from a reference? Commented May 4, 2021 at 1:26
  • 2
    Usually you use references instead of pointers, using pointers in C++ is where things get very messy. What's the goal here that requires pointers? Commented May 4, 2021 at 1:37

2 Answers 2

2

It should be (*table)[0][0].

table[0] is equivalent to (*table) (the vector of vector board).

so table[0][x] is just board[x].

and vector is not streamable.

Sign up to request clarification or add additional context in comments.

Comments

0

You code fails to compile for reason described by others, that you actually do not dereference a pointer (*table)[0][0], in result expression returns wrong type

A C++ reference is an alias of original object "bound" to it, it's literally not possible to get address or value of reference itself, you would obtain address and value of bound object. In some cases reference doesn't have memory representation at all, on others it's actually an address, but standard doesn't define that. In case of function's argument passed by reference, the object represented by argument expression is bound to to the name of function's parameter, in your case it's the board, until end of function call. After that, if object was temporary and we could somehow store the reference, reference would be dangling:

void foo (Type& a)
{
    static Type& ref  = a; // BAD idea. this initialization happens only once
    // but on second call `ref` may refer to non-existing instance of Type
}

If you for some odd reason need a container that behaves like reference, there is std::reference_wrapper, but it's a class which rarely used directly, rather it is used when implementing containers.

PS. You have another problem, by default char is treated as a letter by streams. You probably would want to store elements of sudoku as a enum or another integral type , or keep in mind that you have to cast.

PPS. I wouldn't use std::vector<std::vector<T>> type here. I would rather go with OOP style and create class that encapsulates board functions, e.g. (it's a sketch laking of some details, e.g. fault protection)

class Sudoku {
public:
    enum Number { none = 0, one = 1, two, three, four, five, six, seven, eight, nine};
    
    Sudoku(int N) : rank(N), data(N*N, Sudoku::none) { }
    // add more constructors here if needed?
    
    // would allow to address particular element, e.g. board(i,j) 
    Number operator() (unsigned col, unsigned row) const
    { return data[row*rank + col];  }
    
    Number& operator() (unsigned col, unsigned row)
    { return data[row*rank + col];  }
    
    unsigned size() const {return rank;}
    
    // more operations, resize, a copy operation on board if required?
private:
    unsigned             rank; // size of board
    std::vector<Number>  data; // content of board
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.