我的班级Game有一名成员EntityManager entityManager_.
该类EntityManager有一个私有成员Player player_和Player &EntityManager::getPlayer()返回的公共getter函数player_.
该类Player具有例如函数void startMoving()和sf::Vector2f getPosition() const.
现在,我可以毫无问题地entityManager_.getPlayer().startMoving();从我的Game课堂内打电话,但是当我尝试以下代码来获得玩家的位置时:
sf::Vector2f playerPosition = entityManager_.getPlayer().getPosition();
我收到以下错误:
智能感知:
EntityManager Game::entityManager_
Error: the object has type qualifiers that are not compatible with the member function
object type is: const EntityManager
Run Code Online (Sandbox Code Playgroud)
输出:
game.cpp(261): error C2662: 'EntityManager::getPlayer' : cannot convert 'this' pointer from 'const EntityManager' to 'EntityManager &'
Conversion loses qualifiers
Run Code Online (Sandbox Code Playgroud)
我尝试const …
我想检查我的程序是否有内存泄漏,并发现了这篇微软文章.
我完全按照文章加入了
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
Run Code Online (Sandbox Code Playgroud)
和
_CrtDumpMemoryLeaks();
Run Code Online (Sandbox Code Playgroud)
当程序退出时.
它正确地在我的输出窗口中转储所有内存泄漏信息,但这是问题所在:
它不会打印内存泄漏的文件名和行号!
它在文章中#define _CRTDBG_MAP_ALLOC说它用它打印文件名和行号,但它不适合我.
我的输出看起来像这样
Detected memory leaks!
Dumping objects ->
{3456} normal block at 0x038F81E8, 560 bytes long.
Data: < A B> 00 00 00 00 00 00 10 41 00 00 00 FF 00 00 E6 42
{3447} normal block at 0x038F8170, 56 bytes long.
Data: < B ^ B > 80 42 90 03 10 02 5E 08 80 42 90 03 …Run Code Online (Sandbox Code Playgroud) 我目前正在开发平台并尝试实现时间步长,但对于超过60的帧速率限制,CPU使用率从1%上升到25%甚至更高.
我制作了这个最小程序来演示这个问题.代码中有两条注释(第10-13行,第26-30行)描述了问题以及我测试过的内容.
请注意,FPS的东西与问题无关(我认为).
我试着让代码简洁明了:
#include <memory>
#include <sstream>
#include <iomanip>
#include <SFML\Graphics.hpp>
int main() {
// Window
std::shared_ptr<sf::RenderWindow> window;
window = std::make_shared<sf::RenderWindow>(sf::VideoMode(640, 480, 32), "Test", sf::Style::Close);
/*
When I use the setFramerateLimit() function below, the CPU usage is only 1% instead of 25%+
(And only if I set the limit to 60 or less. For example 120 increases CPU usage to 25%+ again.)
*/
//window->setFramerateLimit(60);
// FPS text
sf::Font font;
font.loadFromFile("font.ttf");
sf::Text fpsText("", font, 30);
fpsText.setColor(sf::Color(0, 0, 0));
// FPS …Run Code Online (Sandbox Code Playgroud) 嘿,我想在我的SFML应用程序中绘制一个精灵,但当我使用图像和类成员的纹理时,它的纹理总是白色的
班级成员:
sf::Sprite myimg;
sf::Image myimg_image;
sf::Texture myimg_texture;
Run Code Online (Sandbox Code Playgroud)
然后我在我的cpp文件中创建这样的精灵
// create image
myimg_image.create(icon.width, icon.height, icon.pixelData);
// create texture from image
myimg_texture.create(icon.width, icon.height);
myimg_texture.update(myimg_image);
// apply texture to sprite
myimg.setTexture(myimg_texture);
Run Code Online (Sandbox Code Playgroud)
当我用window.draw(myimg)绘制它时,它只绘制一个白色精灵
(图标是包含图像信息的结构..它只是我想用GIMP导出为C源的图像)
我进行了一些实验,并意识到当我创建上面提到的类成员不是作为类成员而是作为main函数中的局部变量时,图像被正确绘制...
但这对我没有帮助,因为我需要它们作为成员,因为我也希望从其他功能中访问它们
你能帮帮我吗我只是不知道该怎么做了:(
我有一个类TileGrid,它保存std::vector< std::vector<Tile> >.访问Tile向量中的对象有效,但我无法更改其属性?为了完成,这里是所有相关的类:
tilegrid.h
#include <vector>
#include "tile.h"
class TileGrid {
public:
TileGrid();
TileGrid(unsigned int rows, unsigned int cols);
virtual ~TileGrid();
unsigned int getRows() const { return rows_; };
unsigned int getCols() const { return cols_; };
Tile atIndex(unsigned int row, unsigned int col) const { return tiles_[row].at(col); };
private:
std::vector< std::vector<Tile> > tiles_;
unsigned int rows_;
unsigned int cols_;
};
Run Code Online (Sandbox Code Playgroud)
tilegrid.cpp
#include "tilegrid.h"
TileGrid::TileGrid() : rows_(0), cols_(0) {
}
TileGrid::TileGrid(unsigned int rows, unsigned …Run Code Online (Sandbox Code Playgroud) 我有一个像我这样在我的类中声明的fstream对象(只是一个例子):
class Asd {
public:
Asd();
private:
std::fstream stream;
};
Run Code Online (Sandbox Code Playgroud)
现在,当调用构造函数时,我想像这样指定fstream参数
Asd::Asd() {
this->stream = std::fstream(file, std::fstream::in);
}
Run Code Online (Sandbox Code Playgroud)
然后在我拥有的所有类函数中使用该流,但它不起作用.VS给我的一个错误是:
no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
Run Code Online (Sandbox Code Playgroud)
所以我读到了这一点,我所能找到的是我不能(或者更确切地说:不应该)复制流,事实上我甚至不想这样做.有人说可以将它添加到构造函数中:
Asd::Asd() : stream(file, std::fstream::in) {
...
}
Run Code Online (Sandbox Code Playgroud)
但它打印相同的错误,我不知道该怎么办...还有别人说我必须引用该对象,但我不知道如何?我只想要这个工作,但我想不出来:(
编辑:这是完整的错误消息
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(860): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : …Run Code Online (Sandbox Code Playgroud) sf::RenderWindow::getPosition()0, 0当我从创建窗口的同一范围调用它时,总是返回而不是正确的位置.
以下是重现问题的示例代码:
#include <iostream>
#include <memory>
#include <SFML/Graphics.hpp>
int main() {
std::unique_ptr<sf::RenderWindow> window_;
window_ = std::unique_ptr<sf::RenderWindow>(
new sf::RenderWindow(sf::VideoMode(800, 600), "asd", sf::Style::Default)
);
window_->setFramerateLimit(30);
window_->setVerticalSyncEnabled(false);
while (window_->isOpen()) {
sf::Event evt;
while (window_->pollEvent(evt)) {
if (evt.type == sf::Event::Closed) { window_->close(); }
}
window_->clear();
window_->display();
}
const sf::Vector2i wpos = window_->getPosition();
std::cout << "window position: " << wpos.x << "/" << wpos.y <<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是:我有一个MainMenu我传递的类sf::RenderWindow *(带window_.get()).
当我打电话window_->getPosition()给MainMenu它报告正确的位置时,所以getPosition() …
在C++中,当我说 int i = 0;
现在是int signed int i = 0;还是unsigned int i = 0;?
我没有通过谷歌找到答案
我std::vector<int>的大小为10,每个条目最初为-1.此向量表示我的游戏的排行榜(高分),-1表示该条目没有得分.
std::vector<int> myVector;
myVector.resize(10, -1);
Run Code Online (Sandbox Code Playgroud)
当游戏开始时,我想从文件中加载高分.我加载每一行(最多10行),将找到的值转换为intwith std::stoi,如果数字> 0,则将其替换为当前位置向量中当前的-1.
这一切都有效.现在来问题:
由于文件中的值不一定排序,我想myVector在加载所有条目后进行排序.我这样做
std::sort(myVector.begin(), myVector.end());
Run Code Online (Sandbox Code Playgroud)
这按升序排序(在我的游戏中分数越低越好).
问题是,由于向量最初用-1填充,并且高分文件中不一定保存10个条目,因此除了玩家的分数之外,向量可能包含几个-1.
这意味着当使用上面的代码对矢量进行排序时,所有-1将出现在玩家的分数之前.
我的问题是:如何对向量进行排序(按升序排序),但所有带-1的条目都将放在最后(因为它们不代表真实分数)?