草庐IT

hljs-operator

全部标签

c++ - std::vector::operator[] 不测试类型

我有一个这样的结构:structVrtxPros{longidx;std::vectorpros;VrtxPros(constlong&_idx=-1,conststd::string&val=""):idx(_idx){if(!val.empty()&&val!="")pros.push_back(val);}};后来在代码中我这样使用它:longidx=1234;VrtxProsvp(2134,std::string("-1"));if(margin)vp.pros[0]=idx;编译器对此没有问题。我想知道,因为运营商应该提供引用。我couldnotfindstd::string

c++ - 使用转换的 operator+ 的模糊重载

我得到了一个转换konw->int,double->konw的类:classkonw{doublere,im;public:konw():re(0.0),im(0.0){}konw(doubler,doublei=0.0):re(r),im(i){}operatorint(){returnre;}konwoperator+(konwa){konwwynik;wynik.re=re+a.re;wynik.im=im+a.im;returnwynik;}};主要是我使用重载的operator+测试这些转换konwzesp(3.1,0.6);intssuma=zesp+6;编译器在处理最后一

C++ 编译错误 - ‘operator=’ 不匹配

下面的代码编译没有问题classMyClass{public:MyClass(){std::cout但是当我将赋值运算符参数更改为非常量时编译器打印错误:MyClass&operator=(MyClass&m){std::cout我想知道原因。提前致谢。 最佳答案 因为MyClass&使没有const的operator=不是正确的赋值运算符。它必须是operator=(constMyClass&)(或operator=(MyClass)但不要那样做,除非你知道自己在做什么,copy-&-swap。..).否则,您的代码d=MyCla

c++ - boost spirit : Difference between operators "%=" and "="

我不明白这两个运算符之间的区别。让我们举一个例子,将像"AA,BB,CC,DD"这样的输入解析成字符串vector。namespaceqi=boost::spirit::qi;classmy_grammar:publicqi::grammar{public:my_grammar():base_type(start){usingqi::_1;usingqi::char_;start=*(char_-qi::lit(','));}qi::rulestart;};据我所知,a%=b等同于a=b[_val=_1]。这很清楚。但另一方面,解析器*(char_-qi::lit(','))具有std

c++ - 为什么继承的 protected operator=() 具有公共(public)访问权限

声明为protected的重载运算符=对于继承父类作为public的子类是公开可访问的。#includeclassA{public:A(charc):i(c){}chari;protected:A&operator=(constA&rdm){std::cout编译时没有错误:$g++-Wall-otest_operator~/test_operator.cpp$./test_operatora.i==aaccessingoperator=()a.i==x直接使用A是编译不过的。operator=()以外的任何其他运算符重载都不会编译。使用g++4.4.7和7.3.0以及c++98和c+

c++ - "Socket operation on non-socket"错误由于奇怪的语法

我在调用connect时在我的一些网络代码中遇到错误Socketoperationonnon-socket并花了很多时间试图找出导致的原因它。我终于发现是以下代码行导致了问题:if((sockfd=socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol)看到问题了吗?该行应该如下所示:if((sockfd=socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol))我不明白的是为什么第一行不正确的行没有产生警告。换句话说,一般形式不应该:if(foo=bar()编译器看起来很奇怪,尤其是使

c++ - 在 switch 语句中使用类类型 : is it better than using typeid operator?

我在下面看到了有关C++标准$6.4.2中switch语句的内容。Switch语句可以带一个条件。Theconditionshallbeofintegraltype,enumerationtype,orofaclasstypeforwhichasingleconversionfunctiontointegralorenumerationtypeexists(12.3).Iftheconditionisofclasstype,theconditionisconvertedbycallingthatconversionfunction,andtheresultoftheconversion

c++ - 这个 operator() 语法有什么作用?

这段代码取自http://drdobbs.com/cpp/184403774:templateclassMinResult{L&lhs_;R&rhs_;public:operatorL&(){returnlhs_上面的代码试图在箭头指向的线上做什么?我是C++的初学者,我知道我们可以通过定义operator()来覆盖/定义它。但是不应该这样定义吗L&operator(){returnlhs_我确信这是一些不同的语法,因为operator()应该是一个词。此外,您不能用不同的返回类型定义其中两个。 最佳答案 不,这是类型转换运算符。你

c++ - OpenCV 2.1:ostream operator<< cv::Mat 在哪里?

正在关注thisquestion,我试图将cv::Mat的内容打印到标准输出:#include#include#include#include#includeintmain(){cv::Matm=cv::Mat::ones(10,10,CV_32S);std::cout这会导致错误error:nomatchfor‘operator我在Ubuntu11.10上使用gcc4.6.1,并安装了opencv,不包括示例,遵循theseinstructions.我的问题是,运算符在2.1中可用吗?如果可用,我如何获得它? 最佳答案 更新到Op

c++ - 类没有成员 "operator<<"

我已通读Shouldoperator和OverloadingInsertionOperatorinC++,看起来像类似的问题,但没有解决我自己的问题。我的头文件:usingnamespacestd;classAnimal{private:friendostream&operator我的客户:usingnamespacestd;intAnimal::getnumber(){returnnumber;}ostream&Animal::operator实现很简单,但我收到错误:在cpp中-错误:类“Animal”类没有成员“operator我真的不明白,因为我已经将插入运算符声明为Anima