草庐IT

c++编译错误: ISO C++ forbids comparison between pointer and integer

我正在尝试BjarneStroustrup的C++书籍第三版中的一个示例。在实现一个相当简单的函数时,我得到以下编译时错误:error:ISOC++forbidscomparisonbetweenpointerandinteger这可能是什么原因造成的?这是代码。错误在if行:#include#includeusingnamespacestd;boolaccept(){cout>answer;if(answer=="y")returntrue;returnfalse;}谢谢! 最佳答案 您有两种方法可以解决此问题。首选方法是使用:s

c++ - boost Shared_pointer NULL

我使用reset()作为我的shared_pointer的默认值(相当于NULL)。但是如何检查shared_pointer是否为NULL?这会返回正确的值吗?boost::shared_ptrblah;blah.reset()if(blah==NULL){//Doesthischeckiftheobjectwasreset()?} 最佳答案 用途:if(!blah){//Thischecksiftheobjectwasreset()orneverinitialized} 关于c++-b

c++ - 使用 old_pointer + offset 的 new_pointer 设置 shared_ptr

这是一个智能指针:std::shared_ptrp(newchar[size])它表示填充了原始二进制文件内容的数组。在(并且仅在之后)整个数组从文件复制到RAM之后,我可以解析它,并在此期间检索一些标题信息(一些第一个dwords)。然后是实际数据。在不提供更多上下文的情况下,将提到的共享指针设置为实际数据开头的新地址对我来说很方便。此地址仍在分配的内存中。但是如何设置才不会丢失呢?一个问题是(是/否):是否可以设置p在不调用数据删除的情况下偏移前一个指针? 最佳答案 是的,这是可能的。您可以使用构造函数8,此引用中的别名构造函数

C++ 标准 : dereferencing NULL pointer to get a reference?

这个问题在这里已经有了答案:Isnullreferencepossible?(4个回答)关闭3年前。我想知道C++标准对这样的代码有何规定:int*ptr=NULL;int&ref=*ptr;int*ptr2=&ref;实际上,结果是ptr2为NULL,但我想知道,这只是一个实现细节还是标准中已明确定义?在不同的情况下,对NULL指针的取消引用应该会导致崩溃,但在这里我取消引用它是为了获得一个由编译器作为指针实现的引用,因此实际上并没有对NULL进行实际的取消引用。 最佳答案 取消引用NULL指针是未定义的行为。事实上,标准在注释(

c++ - 为什么将 "pointer to pointer to non-const"转换为 "pointer to pointer to const"是不合法的

将指向非常量的指针转换为指向常数的指针是合法的。那么为什么将指向非const的指针转换为指向const的指针是不合法的呢?例如,为什么下面的代码是非法的:char*s1=0;constchar*s2=s1;//OK...char*a[MAX];//akachar**constchar**ps=a;//error! 最佳答案 来自标准:constcharc='c';char*pc;constchar**pcc=&pc;//notallowed*pcc=&c;*pc='C';//wouldallowtomodifyaconstobjec

c++ - "dereferencing type-punned pointer will break strict-aliasing rules"警告

我使用了一个将enum*转换为int*的代码。像这样的:enumfoo{...}...foofoobar;int*pi=reinterpret_cast(&foobar);编译代码(g++4.1.2)时,我收到以下警告消息:dereferencingtype-punnedpointerwillbreakstrict-aliasingrules我用谷歌搜索了这条消息,发现只有在严格的别名优化开启时才会发生这种情况。我有以下问题:如果我留下带有此警告的代码,它会生成潜在的错误代码吗?有没有办法解决这个问题?如果没有,是否可以从源文件内部关闭严格别名(因为我不想为所有源文件关闭它,也不想为这

go - Go 编程语言中的 "method requires pointer receiver"

我刚刚看到了Go编程语言的介绍,并想尝试写几行。在我尝试在这种情况下使用界面之前,一切正常。我该如何解决?packagemainimport"fmt"typeentityfloat32func(e*entity)inc(){*e++}typeincerinterface{inc()}funcdoSomething(iincer){i.inc()}funcmain(){fmt.Println("Hello,世界")vareentity=3e.inc()doSomething(e)fmt.Println(e)}我得到编译器错误:prog.go:24:cannotusee(typeentit

string - 如何将 'string pointer' 转换为 Golang 中的字符串?

是否可以从指向字符串的指针中获取字符串值?我正在使用gooptpackage处理标志解析并且包仅返回*string。我想使用这些值来调用map中的函数。ExamplevarstrPointer=new(string)*strPointer="string"functions:=map[string]func(){"string":func(){fmt.Println("works")},}//Dosomethingtogetthestringvaluefunctions[strPointerValue]()返回./prog.go:17:14:cannotusestrPointer(ty

pointers - "<type> is pointer to interface, not interface"困惑

我有这个问题,我觉得有点奇怪。看看这段代码:packagecoreinterfacestypeFilterInterfaceinterface{Filter(s*string)bool}typeFieldFilterstruct{KeystringValstring}func(ff*FieldFilter)Filter(s*string)bool{//Somecode}typeFilterMapInterfaceinterface{AddFilter(f*FilterInterface)uuid.UUIDRemoveFilter(iuuid.UUID)GetFilterByID(iuu

c++ - 我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗?

通过简单地编写if(pointer)来检查指向不是NULL的指针是否安全,或者我必须使用if(pointer!=NULL)? 最佳答案 你可以;空指针被隐式转换为bool值false而非空指针被转换为true。来自C++11标准,bool转换部分:Aprvalueofarithmetic,unscopedenumeration,pointer,orpointertomembertypecanbeconvertedtoaprvalueoftypebool.Azerovalue,nullpointervalue,ornullmember