[英]Forward declaration of struct with constexpr constructor
I would like to add global instances of a struct class to my program that server special meaning. 我想将结构类的全局实例添加到服务器具有特殊含义的程序中。 A MWE that works is
有效的MWE是
// Example program
#include <iostream>
constexpr int x_max = 3;
constexpr int y_max = 4;
typedef struct Position {
constexpr Position(int x, int y) : x(x), y(y) {};
int x;
int y;
inline bool operator!=(const Position &rhs) const {
return (x != rhs.x || y != rhs.y);
}
inline bool inside_grid(const Position &empty) const {
return (x >= 0 && x < x_max && y >= 0 && y < y_max && *this != empty);
}
} Position;
constexpr Position empty = Position(1,1);
int main()
{
Position p1 = Position(2,3);
Position p2 = Position(1,1);
std::cout << "p1.inside_grid(empty) = " << p1.inside_grid(empty) << " and p2.inside_grid(empty) = " << p2.inside_grid(empty) << std::endl;
}
The global constant empty
would have to be passed to every call of method inside_grid
which made me think if I could declare the global at the beginning of the program and modify the inside_grid
method to not take any parameters like so: 全局常量
empty
必须传递给方法inside_grid
每次调用,这使我考虑是否可以在程序开始时声明全局inside_grid
并修改inside_grid
方法以不接受任何参数,例如:
// Example program
#include <iostream>
struct Position;
constexpr Position empty = Point(1, 1);
constexpr int x_max = 3;
constexpr int y_max = 4;
typedef struct Position {
constexpr Position(int x, int y) : x(x), y(y) {};
int x;
int y;
inline bool operator!=(const Position &rhs) const {
return (x != rhs.x || y != rhs.y);
}
inline bool inside_grid() const {
return (x >= 0 && x < x_max && y >= 0 && y < y_max && *this != empty);
}
} Position;
int main()
{
Position p1 = Position(2,3);
Position p2 = Position(1,1);
std::cout << "p1.inside_grid() = " << p1.inside_grid() << " and p2.inside_grid() = " << p2.inside_grid() << std::endl;
}
The problem is that this won't compile due to errors that I cannot really understand: error: variable 'constexpr const Position empty' has initializer but incomplete type error: invalid use of incomplete type 'struct Position'
问题是由于我无法真正理解的
error: variable 'constexpr const Position empty' has initializer but incomplete type error: invalid use of incomplete type 'struct Position'
而无法编译: error: variable 'constexpr const Position empty' has initializer but incomplete type error: invalid use of incomplete type 'struct Position'
Can this problem be resolved? 这个问题可以解决吗?
You can forward declare your inside_grid()
method and define it later, after empty
is created: 您可以向前声明您的
inside_grid()
方法,并在创建empty
之后稍后对其进行定义:
// Example program
#include <iostream>
struct Position;
constexpr int x_max = 3;
constexpr int y_max = 4;
typedef struct Position {
constexpr Position(int x, int y) : x(x), y(y) {};
int x;
int y;
inline bool operator!=(const Position &rhs) const {
return (x != rhs.x || y != rhs.y);
}
bool inside_grid() const;
} Position;
constexpr Position empty = Position(1, 1);
inline bool Position::inside_grid() const {
return (x >= 0 && x < x_max && y >= 0 && y < y_max && *this != empty);
}
int main()
{
Position p1 = Position(2,3);
Position p2 = Position(1,1);
std::cout << "p1.inside_grid() = " << p1.inside_grid() << " and p2.inside_grid() = " << p2.inside_grid() << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.