草庐IT

Duck-typing

全部标签

c++ - 在 C++ 中显示为 "expression must have integral or enum type"的错误消息

我有以下代码,但在这个等式中出现错误:v=p*(1+r)^n.请帮我找出这个错误的原因。#include#includeusingnamespacestd;intmain(){floatv,p,r;intn;cout>p;cout>r;cout>n;v=(p)*(1+r)^n;//hereiamgettingerrormessageas"expressionmusthaveintegralorenumtype"cout 最佳答案 C++115.12-按位异或运算符exclusive-or-expression:and-express

C++ 编译 "error: expected constructor, destructor, or type conversion before ' =' token "

位于同一文件“foo.h”中的非常简单的代码:classXface{public:uint32_tm_tick;Xface(uint32_ttk){m_tick=tk;}}std::mapm;Xface*tmp;tmp=newXface(100);**//Error**m[1]=tmp;**//Error**tmp=newXface(200);**//Error**m[2]=tmp;**//Error**错误是错误:在“=”标记之前需要构造函数、析构函数或类型转换对于每个任务。 最佳答案 C++不是脚本语言。您可以在可执行代码块的范

c++ - 我真的需要返回 Type::size_type 吗?

我经常有一些类,它们大多只是一些STL容器的包装器,如下所示:classFoo{public:typedefstd::vectorVec;typedefVec::size_typesize_type;constVec&GetVec(){returnvec_;}size_typesize(){returnvec_.size()}private:Vecvec_;};我不太确定返回size_type。通常,某些函数会调用size()并将该值传递给另一个函数,另一个函数将使用它并可能传递它。现在每个人都必须包含那个Fooheader,虽然我实际上只是传递一些大小值,无论如何它应该只是unsig

c++ - 二进制 '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)

我正在编写一个比较std::strings的模板类函数。std::string是模板参数。我的问题是我无法用“==”运算符比较两个const字符串,然后我想我创建了两个非常量临时字符串变量来执行比较,但它仍然无法编译。不知道为什么。类VGraph被实例化为VGraphmyGraph;templatesize_tVGraph::find(constV&vert){Vtemp=vert;//(1)for(size_ti=0;i相关函数原型(prototype)templateconstV&VVertex::getVertex(); 最佳答案

C++11 static assert for equality comparable type?

如何static_assert模板类型是C++11中的EqualityComparable概念? 最佳答案 您可以使用以下类型特征:#includetemplatestructis_equality_comparable:std::false_type{};templatestructis_equality_comparable()==std::declval(),(void)0)>::type>:std::true_type{};您将以这种方式进行测试:structX{};structY{};booloperator==(Xcon

c++ - size of datatype 和 sizeof(data type) 的区别

我在学习C++时遇到了以下问题。我只是一个初学者,我很困惑。sizeof()函数不应该返回数据类型的大小吗?为什么数据对象的大小可能与其sizeof()不同?我不明白答案的解释。假设在一台假想的机器中,char的大小是32位。sizeof(char)会返回什么?一)4b)1c)依赖于实现d)机器相关答案:b说明:标准不要求char为8位,但确实要求sizeof(char)返回1。 最佳答案 sizeof运算符以bytes为单位生成类型的大小,其中字节定义为char的大小。所以根据定义,sizeof(char)始终为1,无论bitsc

c++ - 警告 : 'auto' type specifier is a C++11 extension [-Wc++11-extensions]

我在DebianJessie上使用带有Clang的Qt5。要试验通用lambda,在.pro文件中有:CONFIG+=c++14在构建之后我得到了:warning:'auto'typespecifierisaC++11extension[-Wc++11-extensions]为了摆脱这个明显的信息,我做了:QMAKE_CXXFLAGS+=-Wc++11-extensions但我不断收到明显的信息。为什么?如何隐藏? 最佳答案 根据qmake的存储库历史记录,CONFIG+=c++14节在qmake5.4版中添加:https://co

C++ - 为什么 std::function<some_type_t, void> 无效?

在C++中,如果我尝试这样做:std::function然后编译器会抛出错误。为什么是这样?它在许多情况下很有用。一个例子://g++-std=c++17prblm.cpp#include#includetemplateclasssome_callback{public:usingcallback_t=std::function;some_callback(callback_t_myfunc){this->myfunc=_myfunc;}callback_tmyfunc;};usingcallback_with_just_bool=some_callback;usingcallback

c++ - 同名类之间的共享 vtables : call to virtual method crashes when casting to base type

检查下面的更新,我可以重现并需要帮助。我有一个奇怪的崩溃,其中一些方法在除1个地方之外的任何地方都可以正常工作。这是代码:structbase{virtualwchar_t*get()=0;//canbe{returnNULL;}doesn'tmatter};structderived:publicbase{virtualwchar_t*get(){returnSomeData();}};structcontainer{deriveddata;};//thisisapprox.howitisusedinrealprogramvoidoutput(constbase&data){data

c++ - 这个‘type variableofType()’是函数还是对象?

#includeclassname{public:inta;name():a(0){};};voidadd(name*pname){pname=NULL;}intmain(){namevarName();name*pName=newname();add(pName);add(&varName);//errorC2664:'add':cannotconvertparameter1from'name__cdecl*)(void)'to'name*'} 最佳答案 错误在主函数的第一行:namevarName();您不是使用默认构造函数创建