伪代码(这是我的课程):
struct cTileState {
cTileState(unsigned int tileX, unsigned int tileY, unsigned int texNr) : tileX(tileX), tileY(tileY), texNr(texNr) {}
unsigned int tileX;
unsigned int tileY;
unsigned int texNr;
bool operator==(const cTileState & r)
{
if (tileX == r.tileX && tileY == r.tileY && texNr == r.texNr) return true;
else return false;
}
};
Run Code Online (Sandbox Code Playgroud)
然后我有两个容器:
std::list < std::vector <cTileState> > changesList; //stores states in specific order
std::vector <cTileState> nextState;
Run Code Online (Sandbox Code Playgroud)
在程序的某个地方,我想在我的状态交换函数中做到这一点:
if (nextState == changesList.back()) return;
Run Code Online (Sandbox Code Playgroud)
但是,当我想编译它时,我有一些对我来说毫无意义的错误,例如:
/usr/include/c++/4.7/bits/stl_vector.h:1372:58: 来自 'bool std::operator==(const std::vector<_Tp, _Alloc>&, …
好吧,我想将我的C++ 11程序移植到Windows,但似乎在mingw 4.7.1中没有实现stoi和std :: to_string.我知道它已被问及有一个解决方案来编辑一些标题,但在我的mingw版本(4.7.1附带codelite)标题是不同的,没有我必须移动的确切行(可能是因为答案是对于mingw 4.6).
所以我的问题是如何在mingw 4.7上获得这些功能?是否有任何指南在4.7中的标题中有什么变化,或者它可能会包含在4.8中?
当然有boost :: lexical_cast,但我想保持我的代码不变,所以我正在寻找解决方案如何在mingw中启用这些功能.
也许有一些自定义mingw发行版,它支持这些功能?
在CPython中,线程模块不使用多个内核,因为它使用全局解释器锁.但是我最近发现标准库中的多处理模块据说可以回避GIL.所以我认为使用该模块可以在CPython中正确使用多个核心,但我想知道我是否正确.
我需要编写一个需要很好地利用多个内核的应用程序,但这不是性能关键所以我可以用Python编写它,但我需要知道这个模块是否允许我使用多个内核?
我编写了一个简单的观察者模式,其中观察者具有"void notify(std :: string)"函数,并且可观察对象在每个注册的观察者上调用它,并使用标记化的字符串来传输数据.这非常简单,容易且有效,但我需要前进一步.
我需要用信号和插槽来实现它(例如使用boost :: signals2).但是我不知道插槽和信号应该是什么样子以及它们应该如何放置.我也不知道如何允许注册我想要的任何功能,而不仅仅是void(字符串).
我找不到任何使用信号和插槽的好资源.然而每个人都说信号和插槽对于观察者模式来说是惊人的.你能指导我如何将信号和插槽用于观察者模式吗?
我目前没有信号的实现如下:
class observable
{
public:
void register(observer *);
void unregister(observer *);
protected:
void notifyObservers()
{
for every registered observer
observer.notify(std::string tokenized_string);
}
}
class observer
{
public:
void notify(std::string) = 0;
}
Run Code Online (Sandbox Code Playgroud)
我需要更改此模式以使用信号和插槽,但我不知道它应该如何有用,设计良好且灵活.
我正在跟踪错误,并且遇到了非常奇怪的行为。我有一组指针,当我一一删除它们时,第一个删除了,但是擦除另一个则给了我segfault。我用
size_type erase( const key_type& key );
Run Code Online (Sandbox Code Playgroud)
所以它不能与迭代器有关。我的调试器向我展示了在调用堆栈中:
0 - std::less<cSubscriber *>::operator() //cSubscriber is an abstract base class and I have a set of cSubscriber *
1 - std::_Rb_tree<cSubscriber*, cSubscriber*, std::_Identity<cSubscriber*>, std::less<cSubscriber*>, std::allocator<cSubscriber*> >::equal_range
2 - std::_Rb_tree<cSubscriber*, cSubscriber*, std::_Identity<cSubscriber*>, std::less<cSubscriber*>, std::allocator<cSubscriber*> >::erase
3 - std::set<cSubscriber*, std::less<cSubscriber*>, std::allocator<cSubscriber*> >::erase
4 - cEventSystem::unsubscribe //my function, it is as follows in the class which has the set as its member
cEventSystem::unsubscribe(cSubscriber * ptr)
{
set.erase(ptr);
}
Run Code Online (Sandbox Code Playgroud)
在基础cSubscriber抽象类中,有一个虚拟析构函数:
virtual ~cSubscriber()
{
eventSystem.unsubscribe(this);
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我不知道它怎么会导致段错误,当没有这样的元素时,擦除应该只返回0。还是尝试从空容器中擦除某些内容时崩溃?(在添加3个差值指针后,我遇到了另一个错误,即set的大小仅为2,但这是另一个故事)。
所以我使用的库有一个枚举(说它的名字LibEnum).我需要有一个std::unordered_set的LibEnum,但我得到的编译错误是没有专门std::hash为它.我可以很容易地写它并且只返回值的数量(第一个元素是0,第二个1等),但是我应该把它放在哪个专门化以及它应该是什么样子?我无法修改库源.
enum LibEnum { A, B, C, D};
std::unordered_set <LibEnum> mySet;
//need std::hash for LibEnum
//how should it look like?
Run Code Online (Sandbox Code Playgroud) class a
{
public:
a() : b(5), a1(10) //will firstly initialize a1 then b, so order here doesn't matter
int a1;
private:
int b;
}
Run Code Online (Sandbox Code Playgroud)
问题是如何更改顺序(在a1之前初始化b)?我必须将公共成员置于私人之上,以便解决方案对我来说不合适.当然在这里我使用整数,问题更复杂,但它只是一个显示我的问题的例子.
enum aaa {a, b, c};
std::map <aaa, int> container;
container[0]; //compilation error
Run Code Online (Sandbox Code Playgroud)
我知道在这种情况下容器是空的,我会得到段错误,但这不是问题.枚举是算术类型,为什么会出现问题呢?
class a
{
private:
std::shared_ptr <std::string> sptr;
public:
void set(std::string & ref)
{
sptr = &ref; //error
}
};
Run Code Online (Sandbox Code Playgroud)
解决方案是什么?我需要保持引用作为参数,我需要私有指针为shared_ptr.
这是我在C++中的代码:
MyClass foo1() {
return MyClass();
}
int MyClass::foo2() {
return 54;
}
Run Code Online (Sandbox Code Playgroud)
问题是,价值是什么:
foo1().foo2();
Run Code Online (Sandbox Code Playgroud)
是值54还是MyClass对象?