I get the following error in my project upon compiling. Tried everything but unable to resolve this.
Reproduced the error here: https://replit.com/join/egfoiwpgig-darshanpandhi
Error
error: constexpr variable 'noOfTiles' must be initialized by a constant expression
Relevant Code
Board.h
# pragma once
class Board {
private:
public:
static constexpr int getTotalNoOfTiles();
};
Board.cpp
# include "Board.h"
constexpr int Board::getTotalNoOfTiles() {
return 21; // calculation simplified for the sake of example
}
Player.h
# pragma once
# include "Board.h"
class Player {
private:
static constexpr int noOfTiles = Board::getTotalNoOfTiles(); // this needs to be constexpr because I will be using noOfTiles as an array size in this same file
};
Question
Isn't Board::getTotalNoOfTiles()
a constant expression since it simply returns 21. Isn't this a compile-time constant?
Reproduced the error here: https://replit.com/join/egfoiwpgig-darshanpandhi