草庐IT

base_of_five_defaults

全部标签

c++ - 错误 : passing const xxx as this argument of xxx discards qualifiers

我在将仿函数从Windows移植到Linux时遇到问题。(传递给STL::map以进行严格弱排序的仿函数)原文如下:structstringCompare{//Utilizedasafunctorforstl::mapparameterforstringsbooloperator()(stringlhs,stringrhs){//Returnstrueiflhs由于linux不支持_stricmp而是使用strcasecmp,我将其更改为:structstringCompare{booloperator()(stringlhs,stringrhs){//Returnstrueiflhs

c++ - 在不使用流的情况下设置 double (ios_base::precision)

有没有办法不使用流来做到这一点?例如,像这样:doublea=6.352356663353535;doubleb=a.precision(5);代替:doublea=6.352356663353535;std::cout.precision(5);std::cout我是C++的新手,我很好奇。提前谢谢。 最佳答案 我已经根据@john、@Konrad和@KennyTM的建议修改了代码。我检查过它是否适用于负数。#include#includeusingnamespacestd;intmain(){doublea=6.35235666

C++ 连接字符串导致 "invalid operands of types ‘const char*’ 和 ‘const char"

我想连接两个字符串,但出现错误,我不知道如何克服这个错误。有什么方法可以将这个constchar*转换为char吗?我应该使用一些取消引用吗?../src/main.cpp:38:error:invalidoperandsoftypes‘constchar*’and‘constchar[2]’tobinary‘operator+’make:***[src/main.o]Error1但是,如果我尝试以这种方式组成“bottom”字符串,它会起作用:bottom+="|";bottom+=tmp[j];bottom+="";这是代码。#include#include#include#inc

c++ - 如何在模板元编程中使用 'default' 值

我面临以下问题:我有一些通用容器,能够对类型执行一些操作。为简单起见,这些操作在需要时是线程安全的。并且,请求意味着容器中的类型具有typedefstd::true_typeneeds_thread_safety;。structthread_safe_item{typedefstd::true_typeneeds_thread_safety;/**/};structthread_unsafe_item{typedefstd::false_typeneeds_thread_safety;/**/};templatecontainer{/*somealgorithms,thatarestd

c++ - 可变参数模板 : Perfect forwarding of integer parameter to lambda

有类似的问题,但我没有找到适合我的问题的答案。考虑以下代码:#include#include#include#include#includeclassTestClass{public:TestClass(intvalue):mValue(value){}private:intmValue;};templateclassDeferredCreator{public:templateDeferredCreator(Args&&...args):mpCreator([=]()->T*{returnnewT(std::forward(args)...);}),mpObject(){}T*get

c++ - 谷歌模拟 : "no appropriate default constructor available"?

将VisualStudio2010C++与googlemock结合使用。我正在尝试使用我创建的模拟,但在线上遇到编译器错误:EmployeeFakeemployeeStub;错误是:1>c:\someclasstests.cpp(22):errorC2512:'MyNamespace::EmployeeFake':noappropriatedefaultconstructoravailable假员工:classEmployeeFake:publicEmployee{public:MOCK_CONST_METHOD0(GetSalary,double());}员工:classEmploy

c++ - 编译错误 : base operand of ‘->’ has non-pointer type ‘Token’

我在尝试编译我的C++代码时遇到标题中提到的错误。我无法理解我在这里做错了什么。编译器在我执行booloperator==(Token)函数时出现问题。我认为这是使运算符(operator)重载的方法。关于为什么编译器不喜欢我提到的任何线索this->terminal还是this->lexeme?classToken{public:tokenTypeterminal;std::stringlexeme;Token*next;Token();booloperator==(Token&t);private:intlexemelength,line,column;};boolToken::o

c++ - 何时使用 =default 与 =delete

据我了解,这些语义仅用于复制构造函数、移动构造函数、复制赋值、移动赋值和析构函数。使用=delete用于禁止使用其中一项功能,即=default如果您想向编译器明确说明在何处使用这些函数的默认值,则使用它。在制作类(class)时使用这些关键字的最佳做法是什么?或者更确切地说,在开发类(class)时我如何记住这些?例如,如果我不知道我是否会使用这些功能之一,最好用delete禁止它。或允许并使用default? 最佳答案 好问题。同样重要的是:哪里使用=default和=delete.我对此有一些有争议的建议。它与我们所有人(包括

c++ - std::result_of 用于内置运算符

通过result_of确定诸如-int()或double()*double()之类的结果的正确语法是什么?失败std::result_of::typestd::result_of::type 最佳答案 std::result_of真的不是这里采取的方法。decltype做你想做的,可以用作decltype(-int()),decltype(double()*double())等等如果你不知道类型是否是默认构造的,你也可以使用std::declval:decltype(-std::declval()).任何语法涉及operator-的

c++ - "= default"是否允许离线实现?

通常我会在header中看到=default语法。我的理解是,这与函数在header中明确实现是一样的,请参见下面的Foo。Foo.h#pragmaonceclassFoo{public:Foo()=default;Foo(constFoo&other)=default;};纯粹出于好奇,=default可以在源文件中使用如下吗?栏.h#pragmaonceclassBar{public:Bar();Bar(constBar&other);};酒吧.cpp#include"Bar.h"Bar::Bar()=default;Bar::Bar(constBar&)=default;据我所知