草庐IT

function-declaration

全部标签

c++ - 确定 std::function 的返回类型

我正在编写一个模板函数,它接收一个std::function对象(通过使用适当的参数调用std::bind生成)。在这个函数中,我想确定这个函数对象的返回类型。有可能吗?事实上,我希望模板函数返回相同的类型。您能想出一种优雅的、基于标准的方法来实现这一目标吗?类似于:templateT::return_typefunctionObjWrapper(TfunctionObject){//...returnfunctionObject();}谢谢 最佳答案 您可以使用decltype和尾随返回类型来完成:templateautofunc

c++ - 将双变量 std::function 转换为单变量

我有一个获取两个值x和y并返回结果的函数:std::functionmult=[](doublex,doubley){returnx*y;};现在我想得到一个常量y的单变量函数。我写了下面的代码,但它不起作用。std::function(std::function,double)>funcYgivenX=[](std::functionfunc2d,doubleinX){return[&func2d,&inX](doubleinY){returnfunc2d(inX,inY);};};我在这里做错了什么?最好(最有效)的方法是什么? 最佳答案

c++ - "error: no matching function for call to"

我当时在键盘上,我正在尝试使用C++来提高我的技能。我以前从未使用过模板,所以我尝试研究如何使用它们。下面的代码是结果,不幸的是,它不起作用。我确实尝试寻找问题的解决方案,但由于我没有太多使用模板的经验,所以我无法在我的问题和其他问题之间建立任何联系。所以,我决定寻求帮助。templateclassVector2{public:Ax,y;Vector2(Axp,Ayp){this->x=xp;this->y=yp;}};templateclassrayToCast{public:rayToCast(Bangle,Vector2origin,Vector2point1,Vector2po

C++ 错误 : forward declaration of 'struct. ..?

循环包含问题我转发声明其中一个类在另一个类的标题中,试图解决它们的循环包含问题。这是我的两个文件:第一个文件(Parameter.h):#pragmaonce#include"Token.h"`classExpression;classParameter{public:Parameter(){string=newToken();identifier=newToken();expr=newExpression();}Token*string;Token*identifier;Expression*expr;};第二个文件(Expression.h):#pragmaonce#include

c++ - C2556 : overloaded function differs only by return type

我正在阅读EffectiveC++,它告诉我“可以重载仅因常量不同而不同的成员函数”。书中的例子是:classTextBlock{public:constchar&operator[](std::size_tposition)const;char&operator[](std::size_tposition);private:std::stringtext;}我下面的示例使用了一个存储指针。classA{public:A(int*val):val_(val){}int*get_message(){returnval_;}constint*get_message(){returnval_

c++ - vector 声明 "expected parameter declarator"

这个问题在这里已经有了答案:Whycan'tmemberinitializersuseparentheses?(2个答案)关闭5个月前。我在类的私有(private)成员变量中有一行代码:vectordQdt(3)在xcode中编译时,会出现错误“expectedparameterdeclarator”。我想我提供了足够的信息。我认为此声明没有任何问题。

c++ - 如何解决 "class must be used when declaring a friend"错误?

classtwo;classone{inta;public:one(){a=8;}friendtwo;};classtwo{public:two(){}two(onei){cout我从Dev-C++收到此错误:aclass-keymustbeusedwhendeclaringafriend但是用MicrosoftVisualC++编译器编译时它运行良好。 最佳答案 你需要friendclasstwo;代替friendtwo;此外,您不需要单独转发声明您的类,因为友元声明本身就是一个声明。你甚至可以这样做://noforward-de

c++ - 继续获取 "error: use of undeclared identifier ' cout' 和错误 : reference to overloaded function could not be resolved

我正在编写一个使用许多不同函数的排序程序,你们都可以从我的声明。但是,当我尝试编译和运行我的程序时,我不断遇到这些相同的错误它们如下:error:useofundeclaredidentifier'cout';didyoumean'count'?couterror:referencetooverloadedfunctioncouldnotberesolved;didyoumeantocallit?couterror:useofundeclaredidentifier'endl';didyoumean'end'?cout我不太确定为什么会出现这些错误....我想我已经包含了我需要的一切为

c++ - 为什么宏 __STL_FUNCTION_TMPL_PARTIAL_ORDER 应该将模板函数包含在 std_pair.h 中

今天在STL_pair.h中看到如下代码:#ifdef__STL_FUNCTION_TMPL_PARTIAL_ORDERtemplateinlinebooloperator!=(constpair&__x,constpair&__y){return!(__x==__y);}templateinlinebooloperator>(constpair&__x,constpair&__y){return__y我不认为模板函数与偏特化有任何关联的功能模板。我错了吗? 最佳答案 编译器如何处理函数调用在C++中调用函数模板经历了名称查找(标准

C++ 设计 : Overloading/Overriding many many functions, 的清理方式?

我在这里尝试实现的案例是一个基类,它有一个函数(我们称之为modify_command),它实际上可以接受许多不同的类型,因此派生类可以实现它们认为合适的modify_command函数。现在我在基类中有一些类似的东西:classBase{templatevoidmodify_command(Commandcmd){std::cout(cmd);//Callsthetemplatedfunction}virtualvoidmodify_command(SpecificCommandBcmd){modify_command(cmd);//Callsthetemplatedfunction