成员函数的可访问性似乎随 SFML 中的范围而变化,C++ Xcode 中也是如此
Accesibility of member functions seems to vary with scope in SFML, C++ with Xcode
虽然我可以在创建sf::RectangleShape对象时访问成员.setPosition,但我似乎无法在不同的范围内访问.setPosition成员。帮助?我是Xcode的新手,但C++熟悉,不确定为什么这会导致错误。
class ShapeVisual : public sf::Drawable, public sf::Transformable {
public:
int fillShape[16];
int shapeWidth;
int shapeHeight;
sf::RectangleShape shapeBlock;
float shapeBlockWidth;
ShapeVisual() {
shapeWidth = 4; shapeHeight = 4;
Tetrominos::SetShape("T", &fillShape);
shapeBlockWidth = 10.0;
shapeBlock = sf::RectangleShape();
shapeBlock.setPosition(0,0);
shapeBlock.setOutlineColor(sf::Color::Green);
shapeBlock.setSize(sf::Vector2f(shapeBlockWidth,shapeBlockWidth));
shapeBlock.setFillColor(sf::Color(255,100,100));
}
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
states.transform *= getTransform();
for (int Bx = 0; Bx < this->shapeWidth; Bx++) {
for (int By = 0; By < this->shapeHeight; By++) {
shapeBlock.setPosition(Bx*shapeBlockWidth, By*shapeBlockWidth);
//ERROR HERE: No matching member call for shapeBlock.setPosition.
if (fillShape[Bx + By*shapeWidth] != 0) {
target.draw(shapeBlock,states);
}
} }
}
};
错误的确切文本是
/Volumes/Minerva/Users/dustinfreeman/Documents/Shapeshifter/Code/Shapeshifter/shapeshifter/shapeshifter/shapes.cpp:147:20: error: no matching member function for call to 'setPosition'
shapeBlock.setPosition(Bx*shapeBlockWidth, By*shapeBlockWidth);
~~~~~~~~~~~^~~~~~~~~~~
/usr/local/include/SFML/Graphics/Transformable.hpp:70:10: note: candidate function not viable: no known conversion from 'const sf::RectangleShape' to 'sf::Transformable' for object argument
void setPosition(float x, float y);
^
/usr/local/include/SFML/Graphics/Transformable.hpp:84:10: note: candidate function not viable: requires single argument 'position', but 2 arguments were provided
void setPosition(const Vector2f& position);
^
下面是 sf::RectangleShape 类的文档: http://www.sfml-dev.org/documentation/2.0/classsf_1_1RectangleShape.php
编辑:我将shapeBlock更改为指针,现在它似乎可以编译并运行良好。但是我找不到原始代码的问题。
您的draw
函数是const
。这意味着您无法修改对象的属性。C++只允许您对属性调用其他const
成员函数。在这种情况下,setPosition
不是const
成员函数,因此无法编译。
显然,当您切换到指针时,您必须执行其他操作才能使其工作。
相关文章:
- C++取消引用指针.为什么会发生变化
- 为什么在读取文件大小时文件IO速度会发生变化
- 为什么cudaMemGetInfo报告设备内存总量的变化
- 如何避免LED在循环状态变化中闪烁?
- 当 I2C 值在C++中发生变化时收到通知
- Gtkmm 会不断检查 Gtk::日历上的所选日期是否发生变化
- 当我调用对象的方法时,对象的成员会发生变化
- 为什么向量内部的指针在从函数返回时会发生变化?
- 如何在 c++ 中循环中使用频率变化的声音?
- 为什么 C++ 中的零会发生变化
- 为什么我的全局变量似乎没有变化?
- 使用 IMFSinkWriter 编码的视频的播放速度会根据宽度而变化
- C++11:具有互斥锁的线程看到原子变量的值发生变化,尽管这是唯一可以改变它的代码
- 为什么预减量会导致奇怪的变化?
- MATLAB:跟踪imufilter对象中的状态变化
- 当父小部件大小发生变化时,如何更改子小部件的大小?
- 我正在将一个 std::string 传递给一个 boost 函数,该函数对该类型进行常量引用,但该值发生了变化
- c++问题:给一个变量赋值后,另一个变量发生了变化
- 为什么使用默认构造函数"{}"而不是"= default"存在性能变化?
- 当对象传递给成员函数时,成员函数的行为会发生变化?