I have a header file that looks something like the following:
class Model {
private:
struct coord {
int x;
int y;
} xy;
public:
....
coord get() const {
return xy;
}
};
And in yet another file (assume ModelObject exists):
struct c {
int x;
int y;
void operator = (c &rhs) {
x = rhs.x;
y = rhs.y;
};
} xy;
xy = ModelObject->get();
The compiler throws an error that says there is no known covnersion from coord to c.
I believe it is because it doesn’t know about coord type because it is declared inside of a class header. I can get around that by declaring the struct outside of the class, but I was wondering if it is possible to do the way I am, or is this generally considered bad practice
You would need an implicit conversion operator for
Model::coordtoc. For information on how to do this, I recommend taking a look at C++ Implicit Conversion Operators.Also, when you mentioned “It does not know the type because it is in a class”, you would use
Model::coordas the struct type to the outside world (as long ascoordis public, which in your current case it is not).