error C2228: left of '.x' must have class/

2019-08-02 07:11发布

问题:

I am getting the error and have absolutely no idea why!

//create a circle shape.
sf::CircleShape shape;
shape.setRadius(25);
shape.setFillColor(sf::Color(100,250,250));

//circle collision geometry
circle circleTest(shape.getPosition.x,shape.getPosition.y,shape.getRadius())

Circle is the class for circle collision geometry.And it fails on the constructor

(shape.getPosition.x,shape.getPosition.y,shape.getRadius())

I don't know why i got the error , it worked fine then all of a sudden gave me the error in the title.

回答1:

getPosition is a function. You must call it and access its return value’s members.



回答2:

The reason why you are getting this error is because sf::Shape::getPosition() is a method and you need to call that method to get the position. After you make that call you can then access the .x and .y members of the sf::Vector2f that sf::Shape::getPosition() returns.

So your code should instead look like this.

//create a circle shape.
sf::CircleShape shape;
shape.setRadius(25);
shape.setFillColor(sf::Color(100,250,250));

//circle collision geometry
circle circleTest(shape.getPosition().x,shape.getPosition().y,shape.getRadius())

sf::Shape::getPosition()



标签: c++ sfml