草庐IT

MAKE_ENUM_OPERATOR_TYPESAFE

全部标签

c++ - "No match for operator="试图在 C++ 中遍历映射

我正在尝试遍历定义如下的map:std::map>ridx_;现在我尝试在以下重载运算符的友元函数中遍历ridx_(它是一个类的私有(private)成员)std::ostream&operator>::iteratorit;//Thefollowingisline34for(it=m.ridx_.begin();it!=m.ridx_.end();it++)osfirst但是g++错误输出:SMatrix.cpp:34:error:nomatchfor'operator='in'it=m->SMatrix::ridx_.std::map::beginwith_Key=unsigned

c++ - std::map 的 int 类型比较的 operator< 重载? (我希望它按降序排序..)

我遇到了一个问题,我想定义一个map,它在内部按第一个的降序排序。如果第一个不是主要类型,比如它是一个类,我可以在该类中重载“非常感谢!! 最佳答案 添加比较器:#includemap>m;默认为less. 关于c++-std::map的int类型比较的operator https://stackoverflow.com/questions/8659744/

c++ - operator new 已经有一个主体

我正在尝试将参数作为全局参数来实现operatornew。如果没有args的new重载没有问题,但是在尝试编译时出现以下错误inlinevoid*operatornew(size_t,void*p){//...returnp;}c:\bjarne_exercise_6.cpp(14):errorC2084:function'void*operatornew(size_t,void*)throw()'alreadyhasabodyc:\programfiles\microsoftvisualstudio10.0\vc\include\new(55):seepreviousdefiniti

c++ - 如何在 Visual Studio 中解决 make_shared 的 10 个限制

在一些旧代码上使用C++10的新功能时,我遇到了无法调用带有12个参数的make_shared的问题。我记得Microsoft的STL谈到他们如何为make_shared使用仿真,并且10是最大值。显然仅仅为此重构代码是不可能的,所以基本上我的问题是-有没有办法在VS2010中为make_shared获取超过10个参数。 最佳答案 make_shared(1,2,3,4,5,6,7,8,9,10,11,12);可以替换为shared_ptr(newfoobar(1,2,3,4,5,6,7,8,9,10,11,12));在C++11中

c++ - 为什么 C++ STL 容器使用 "less than"operator< 而不是 "equal equal"operator== 作为比较器?

在std::map的自定义类中实现比较运算符时,我遇到了这个问题,但看不到任何被问到的地方。除了上述问题,也有兴趣简要了解,如何operator适用于std::map.问题来源:structAddress{longm_IPv4Address;boolisTCP;booloperator 最佳答案 std::map需要能够排序。默认情况下使用std::less,对于非指针使用1。使用您对用户的要求最少的规则,它从综合“等价”当它需要它时(!(a表示a和b是等价的,即两者都不小于另一个)。这使得编写用作map的关键组件的类变得更加容易,

c++ - operator bool() 转换为 std::string 并与 operator std::string() 冲突

在类中声明operatorstd::string时,operatorbool()怎么会导致错误,而且它本身还充当到string的隐式转换?#include#includeusingnamespacestd;classTest{public:operatorstd::string(){cout 最佳答案 您面临的问题(除了operatorstd::string()返回bool之外)是隐式转换在您需要和不需要时触发。当编译器看到s=t时,它会识别以下潜在的std::operator=匹配项://usingstd::stringforco

c++ - 将 operator== 重载为带有模板参数的自由函数的语法是什么?

我有一组多态类,例如:classApple{};classRed:publicApple{};classGreen:publicApple{};以及比较它们的自由函数:booloperator==(constApple&,constApple&);booloperator我正在设计一个可复制的包装器类,它将允许我使用类Red和Green作为STL映射中的键,同时保留它们的多态行为。templateclassCopy{public:Copy(constCat&inCat):type(inCat.clone()){}~Copy(){deletetype;}Cat*operator->(){

c++ - make_pair 是在栈上还是堆上?

如果我从不同的范围将它插入到map中,是否需要分配一对?#include#include#include#includeusingnamespacestd;voidparseInput(intargc,char*argv[],unordered_map*inputs);intmain(intargc,char*argv[]){unordered_map*inputs=newunordered_map;parseInput(argc,argv,inputs);for(auto&it:*inputs){cout*inputs){inti;for(i=1;iinsert(make_pair(

c++ - Visual Studio 2010 中 std::make_shared() 的友元函数(不是 Boost)

如何创建std::make_shared()的友元函数。我试过:classMyClass{public:friendstd::shared_ptrstd::make_shared();//or//friendstd::shared_ptrstd::make_shared();protected:MyClass();};但它不起作用(我使用的是VisualStudio2010SP1) 最佳答案 如何向您的类添加一个静态方法:classFoo{public:staticshared_ptrcreate(){returnstd::shar

c++ - error C2678 : binary '<' : no operator found which takes a left-hand operand. ..(或者没有可接受的转换)

这个问题在这里已经有了答案:HowcanIusestd::mapswithuser-definedtypesaskey?(8个答案)关闭5年前。这是我在map中查找值的代码:boolmyclass::getFreqFromCache(plVariablesConjunction&varABC,vector&freq){std::map>::iteratorfreqItr;freqItr=freqCache.find(varABC);if(freqItr!=freqCache.end()){freq=freqItr->second;returntrue;}}“PlVariablesCon