草庐IT

const-reference

全部标签

c++ - 无法在参数传递中将 'const char*' 转换为 'WCHAR*'

我在文档中写道,用户名、IP和密码必须是constchar*,当我将变量放入constchar时,出现此错误消息。这是我的代码:#include#include#include#includeusingnamespacestd;typedefint(__cdecl*MYPROC)(LPWSTR);intmain(){HINSTANCEhinstDLL;MYPROCProcAdd;hinstDLL=LoadLibrary("LmServerAPI.dll");if(hinstDLL!=NULL){ProcAdd=(MYPROC)GetProcAddress(hinstDLL,"LmSer

c++ - 在 std::map 中使用 std::reference_wrapper

我以为maps和reference_wrappers会很容易,但我被一些奇怪的东西绊倒了:#include#includeintmain(void){std::map>mb;constinta=5;mb[0]=std::cref(a);}这段代码给我以下编译器错误:Infileincludedfromc:/MinGW/x86_64-w64-mingw32/include/c++/bits/stl_map.h:63:0,fromc:/MinGW/x86_64-w64-mingw32/include/c++/map:61,from../test/main.cpp:9:c:/MinGW/x8

c++ - 使用(float&)int可以进行类型修剪,(float const&)int可以像(float)int那样转换吗?

VS2019版本x86。templatefloatget()const{intf=_mm_extract_ps(fmm,i);return(floatconst&)f;}当使用return(float&)f;编译器使用时extractpsm32,...movssxmm0,m32。正确的结果当使用return(floatconst&)f;编译器使用时extractpseax,...movdxmm0,eax。错误的结果T&和Tconst&首先是T,然后是const的主要思想。const只是程序员的某种协议(protocol)。您知道您可以解决它。但是汇编代码中没有任何const,只能输入f

c++ - 当字符串超出范围时,将 string::c_str() 分配给 const char*

我对基本的C++用法有疑问。下面的代码使用gcc/LInux编译,打印正确。字符串test超出范围,所以它的c_str()值也应该无效,不是吗?我错了还是我误解了constchar*的意思?#includeintmain(){constchar*a="aaaa";std::cout 最佳答案 你是对的,你的代码无效,因为它使用了一个生命周期已经结束的对象。它是“偶然”起作用的,你不能依赖它。 关于c++-当字符串超出范围时,将string::c_str()分配给constchar*,我们

c++ - 为什么这给我 "Undefined references to constructors and destructors"?

我有以下代码:Leomedia.h#include"MusicMetaDatter.h"#ifndefLEOMEDIA_H#defineLEOMEDIA_HnamespaceLeomedia{typedefenum{read,write}FileMode;}#endif//LEOMEDIA_HMusicMetaDatter.h#ifndefMUSICMETADATTER_H#defineMUSICMETADATTER_H#include"Leomedia.h"#include/***METADATTER*@Version1.0.0a*@Author:SergioAndrésIbañez

c++ - 从 const 方法调用成员的非常量方法

我很惊讶地发现“常量”中的这个“洞”:#includeclassA{intr;public:A():r(0){}voidnonconst(){puts("Iaminurnonconstmethod");r++;}};classB{Aa;A*aPtr;public:B(){aPtr=newA();}voidgo()const{//a.nonconst();//illegalaPtr->nonconst();//legal}};intmain(){Bb;b.go();}所以基本上从const方法B::go(),你可以调用non-const成员函数(恰如其分地命名为nonconst())如果

c++ - 这个 C++ const 引用访问器接口(interface)习惯用法有什么问题吗?

我正在将一个结构体转换为一个类,这样我就可以为我的变量强制执行一个setter接口(interface)。不过,我不想更改所有读取变量的实例。所以我转换了这个:structfoo_t{intx;floaty;};为此:classfoo_t{int_x;float_y;public:foot_t():x(_x),y(_y){set(0,0.0);}constint&x;constfloat&y;set(intx,floaty){_x=x;_y=y;}};我对此很感兴趣,因为它似乎模拟了C#的公共(public)只读属性的想法。编译正常,我还没有发现任何问题。除了在构造函数中关联const

c++ - 对 'QNetworkAccessManager' 的 undefined reference

我是QTAPI和QTIDE的新手,我正在学习本教程:http://developer.nokia.com/community/wiki/Creating_an_HTTP_network_request_in_Qt,但是当我尝试编译时出现此错误,问题的通常原因是:编译器无法找到方法代码所在的.cpp/.lib存档,但在这种情况下,api已经配置好了,我不明白为什么会出现此错误,而且我找不到项目属性,这是我的测试代码:#include#include#include#include#include这是mainwindow.hpp文件中包含的header,现在是mainwindow.cpp:

c++ - 无法在 C++ 中调用 const 引用参数的方法

classA{public:A(){};~A(){};voidmethod(){};};voidcall(constA&a){a.method();//IcannotcallthismethodhereifIuse"const"butIcancallitifnotusing"const"}intmain(){Aa;call(a);return0;}在这种情况下,错误是:“将constA作为voidA::method()的参数传递会丢弃限定符[-fpermissive]|”在函数call中,如果我使用const,我会得到错误,但如果我去掉它,它就会工作。谁能帮我解释一下?

c++ - 没有 main() 的 Cpp 类中的 `undefined reference to ` main`

我遇到了this在试图得到答案时。但似乎张贴者有多个文件,但没有链接,因此出现错误。但是,为什么在使用单个文件时会出现此错误?g++myClass.cpp/usr/lib/gcc/i686-redhat-linux/4.6.3/../../../crt1.o:Infunction`_start':(.text+0x18):undefinedreferenceto`main'collect2:ldreturned1exitstatus为什么main在编译时是必需的(它从哪里找到我的代码中提到的main)?main是代码执行的起点,但为什么编译器会假设我在这里需要一个main。我可以在其他