我正在编写一个用于处理多项式的小型库。
\n主类称为poly,它包含 4 个重载的构造函数。
类的对象poly是一个多项式的表示。
完整代码可以在这里查看: https: //github.com/IQ8QI/polynomial-lib
\n在./test-poly/test-all.cpp我试图创建一个类的对象poly:
poly::poly my_poly = poly::poly((double)6.0, -2.0, 4.0);\nRun Code Online (Sandbox Code Playgroud)\n类的构造函数poly有:
//Create polynomial using just numbers\npoly(double values...);\n\n//Create polynomial using vector<double>\npoly(vector<double> values);\n\n//Create polynomial with non-zero base\npoly(int base, double values...);\n\n//Create polynomial with non-zero base and vector\npoly(int base, vector<double> values);\nRun Code Online (Sandbox Code Playgroud)\n不幸的是,我收到一个编译错误:
\n./test-all.cpp:20:63: error: call of overloaded \xe2\x80\x98poly(double, double, double)\xe2\x80\x99 is ambiguous\n 20 | poly::poly my_poly = poly::poly((double)6.0, -2.0, 4.0);\n …Run Code Online (Sandbox Code Playgroud) 有可能做一些事情:
type t = int;//this would be a function which identifies what type the next argument is
if( t == int )
printf( "%d", va_arg( theva_list, t ) );
Run Code Online (Sandbox Code Playgroud)
以一种相对微不足道的方式?我知道唯一可以保存类型的对象是type_info,我无法弄清楚如何以这种方式使用它.
谢谢,帕特里克
我正在尝试在C++中创建一个可变长度函数(显然,heh),而我现在所拥有的功能,但仅适用于第一个参数.如果有人可以请让我知道如何使用所有通过的论点,我会非常感激.
码:
void udStaticObject::accept( udObjectVisitor *visitor, ... )
{
va_list marker;
udObjectVisitor *i = visitor;
va_start( marker, visitor );
while( 1 )
{
i->visit_staticObject( this );
//the if here will always go to the break immediately, allowing only
//one argument to be used
if( ( i = va_arg( marker, udObjectVisitor* ) ) )
break;
}
va_end( marker );
}
Run Code Online (Sandbox Code Playgroud)
根据我过去发布的帖子以及我提供的任何帮助帖子,可能会提供一些我没有提供的信息,您需要知道这些信息才能提供帮助.如果我忘了什么,我会提前道歉,请告诉我您需要知道的内容,以便我提供相关信息.
我正在寻找一种方法,以便我可以使用相同的代码部分来处理通过Set或variadic参数传递的参数.例如
public void func(String...strs){
for (String str : strs){
//Deal with str
}
}
Run Code Online (Sandbox Code Playgroud)
根据规范,func还将支持:
public void func(Set<String> strs){
for (String str : strs)
//Deal with str
}
}
Run Code Online (Sandbox Code Playgroud)
}
两个处理代码都是相同的,我如何合并到单个实现?请提出建议.
谢谢.
问候,威廉
我正在读一本关于java的书,作者做了一些变量论证.它是这样的:
public int num(int ... nums){}
Run Code Online (Sandbox Code Playgroud)
我做了一些研究,它看起来就像nums一个数组.所以我认为上面的代码可以替换为:
public int num(int[] nums){}
Run Code Online (Sandbox Code Playgroud)
我的问题:变量参数有什么意义?你能改变其他类型的类型String吗?
我试图重新编码一个printf函数,所以我需要使用,va_arg因为printf是一个可变函数.
问题是,当我想打印出参数时my_printf,如果我们采用这两种基本可能性,它们可以是%i(int)或%s(char *).
因此,当我浏览我的时候va_list ap,我试图在va_arg函数中添加一个变量,以便我可以更改要写入的类型,如下所示:
char *str = NULL;
str = ft_set_my_types(flags[cur_arg]);
ft_putstr(va_arg(ap, str));
Run Code Online (Sandbox Code Playgroud)
我的功能ft_set_my_types是这样的:
char *ft_set_my_types(char *flags)
{
char **tab = NULL;
tab = ft_alloc_mem2(2, 15); /* this function malloc a tab */
char a = 65;
/* the example here is only with two types of variables */
/* I am sending flags %s in my …Run Code Online (Sandbox Code Playgroud) void method(String[] a)和之间有什么区别void method(String... a)?
第一种方法采用字符串数组,其中第二种方法采用一个或多个String参数.它们提供了哪些不同的功能?
此外,不知道为什么,但这是有效的:
public class Test {
public static void main(String[] args) {
String[] a = {"Hello", "World"};
Test t = new Test();
t.method(a);
}
void method(String...args) {
System.out.println("Varargs"); // prints Varargs
}
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个图形窗口的代码,其中包含几个运行回调函数的GUI项.目前我有一个文本框和一个滑块控件,我进行了设置,以便更改文本框值不仅会更改显示的数据,还会更新滑块位置以匹配该值.我varargin遇到的问题是试图保持我的参数(在创建图形窗口时输入到main函数)被包含在父cell变量中.当我的顶层varargin包含一个值参数对,varargin是一cell维的1x2的.没关系.通常,可以将所有这些参数传递给内部函数调用,如下所示:
function topfunc(varargin)
%code
do_something(varargin{:})
Run Code Online (Sandbox Code Playgroud)
该函数dosomething可以看到正确的参数列表.但是,当我通过callback定义传递varargins时,就像在
txtui = uicontrol(hf,'Style','edit','string',fristframe,'backgroundcolor','y',...
'position',[10 100 50 20],'Tag','Scaler','UserData',lsatframe,...
'Callback',{@doslide,adcname,mode,goodframes,{varargin{:}} } );
Run Code Online (Sandbox Code Playgroud)
然后该函数 doslide调用绘图更新功能dordplot
function doslide(theui,event,fileName,mode, goodframes,varargin)
% code snipped...
dordplot(slidinfo,event,fileName,mode,goodframes,varargin{:});
end
Run Code Online (Sandbox Code Playgroud)
里面doslide,varargin是含有期望的1×2单元1x1的细胞.
我在我的最终函数中添加了一个kludge修复程序,其中varargin实际使用的内容与行
varargin = varargin{:};
Run Code Online (Sandbox Code Playgroud)
但是,varargin首先将它包裹在一个细胞内似乎是错误的.
有没有修复,或者我是否错误地定义了我的回调函数调用?
C中是否有任何方法可以使用可选参数的函数?如果没有那么系统调用如何在参数列表可变的情况下工作?
我想为printf()我正在研究的输出终端是串口的嵌入式板写一个快速的功能.我试过这样的事情:
int32_t printfDebugSerial(const char *format, ...)
{
char tempBuff[256];
memset(tempBuff, 0, sizeof tempBuff);
va_list arg;
int32_t done;
va_start (arg, format);
done = (int32_t)sprintf(tempBuff,format, arg);
va_end (arg);
HAL_sendToSerial((uint8_t*)tempBuff, strlen(tempBuff)); // writes bytes to serial port
return done;
}
Run Code Online (Sandbox Code Playgroud)
但是当我调用它时,我得到的输出如下:
printfDebugSerial("Hello = %u", 1234);
Run Code Online (Sandbox Code Playgroud)
输出:
Hello = 536929228
Run Code Online (Sandbox Code Playgroud)
然后称为:
printfDebugSerial("Hello = %f", 934.3245);
Run Code Online (Sandbox Code Playgroud)
输出:
Hello = 0.000000
Run Code Online (Sandbox Code Playgroud)
任何帮助,这里有什么问题?
c ×3
c++ ×3
java ×3
arrays ×1
callback ×1
cell-array ×1
function ×1
matlab ×1
matlab-gui ×1
typeinfo ×1