我目前正在尝试将std :: unique_ptr存储在std :: unordered_map中,但是我得到了一个奇怪的编译错误.相关代码:
#pragma once
#include "Entity.h"
#include <map>
#include <memory>
class EntityManager {
private:
typedef std::unique_ptr<Entity> EntityPtr;
typedef std::map<int, EntityPtr> EntityMap;
EntityMap map;
public:
/*
Adds an Entity
*/
void addEntity(EntityPtr);
/*
Removes an Entity by its ID
*/
void removeEntity(int id) {
map.erase(id);
}
Entity& getById(int id) {
return *map[id];
}
};
void EntityManager::addEntity(EntityPtr entity) {
if (!entity.get()) {
return;
}
map.insert(EntityMap::value_type(entity->getId(), std::move(entity)));
}
Run Code Online (Sandbox Code Playgroud)
这是编译错误:
c:\program files (x86)\microsoft visual studio 12.0\vc\include\tuple(438): error C2280: 'std::unique_ptr<Entity,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' …Run Code Online (Sandbox Code Playgroud) 我在DLL中有这个代码:
Game::Game(int width, int height)
: world(width, height),
renderer("Game", 800, 600),
font(),
text(),
keyboard()
{
// Code
}
Run Code Online (Sandbox Code Playgroud)
keyboard是一个指向Keyboard对象的指针.调用构造函数中的代码,但键盘对象保持为null.为什么没有创建对象?
键盘构造函数:
Keyboard() {
A = new Key('a');
B = new Key('b');
C = new Key('c');
D = new Key('d');
E = new Key('e');
F = new Key('f');
G = new Key('g');
H = new Key('h');
I = new Key('i');
J = new Key('j');
K = new Key('k');
L = new Key('l');
M = new Key('m');
N = new Key('n');
O …Run Code Online (Sandbox Code Playgroud) 我有一个功能:
bool basicArithmetic(std::function<int(int, int)> func) {
}
Run Code Online (Sandbox Code Playgroud)
它应该返回,true如果func是一个实例std::plus或std::minus但我不知道如何检查.我无法使用,dynamic_cast因为它不是指针.
所以,我不能像这样编译我的代码:
std::vector<std::string> split = split("A String Blah");
Run Code Online (Sandbox Code Playgroud)
使用此方法签名:
std::vector<std::string> split(const std::string& s)
Run Code Online (Sandbox Code Playgroud)
因为它说它需要不止一个参数.为什么不只是一个字符串呢?