草庐IT

operator_name

全部标签

c++ - operator>> 适用于 Visual C++ 2010 但不适用于 Linux 上的 G++

我有以下问题:我的代码适用于VisualC++2010,但是当我在Linux上编译它时,它被编译了,但是有些东西不起作用:这是我的Vector输入operator>>:istream&operator>>(istream&in,Vector&x){chara;in.sync();a=in.get();//getsthe'['for(inti=0;i>x._vector[i];if((i+1)!=x._n)a=in.get();//getsthe','}in>>a;//getsthe']'returnin;}_vector指向一个Complex数组,Complex的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++ 编译错误 - 命名空间 std 中的 "no type named ' 函数”

我正在为我的C++编程类(class)作业,其中涉及实现HashMap。我的导师给了我们一个头文件,我们需要将其与我们的HashMap类一起使用。提供的头文件包含以下行:typedefstd::functionHashFunction;根据我对C++的(有限的)理解,这会将HashFunction类型定义为std::function。但是,当我编译代码时,出现错误:./HashMap.h:46:15:error:notypenamed'function'innamespace'std'typedefstd::functionHashFunction;~~~~~^./HashMap.h:

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++ 宏之谜 : Printing the name of the TYPE

在宏中,我可以使用xxxx_##TYPE和##TYPE##_xxxxx来正确填写TYPE名称,但我不能在字符串中间使用##TYPE##,例如(打印“##TYPE##是类型的名称”;)有解决办法吗? 最佳答案 您可以通过结合两个特征来做到这一点。一种是“字符串化”,即通过在宏参数前加上#前缀将其转换为字符串。(这与您显然已经熟悉的“标记粘贴”运算符##相关但不同。)另一个事实是,当连续给定多个字符串文字时,C++会将它们组合起来成一个字符串。例如,"a""b""c"等同于"abc"。我不清楚你的宏到底是如何定义的,所以我不能告诉你要输