简体   繁体   English

无法从void转换为int

[英]Cannot convert from void to int

I'm trying to call functions through their pointers. 我试图通过它们的指针来调用函数。 Compiling this code, I get errors saying that expressions of type void cannot be converted to other types, but I check whether the function returns a void or not before calling the function. 编译此代码时,出现错误,指出无法将类型为void的表达式转换为其他类型,但是在调用函数之前,我先检查函数是否返回void。 Is there another way that I can accomplish this? 我还有另一种方法可以做到这一点吗?

class FuncBase {
public:
FuncBase(string n, string ret, string arg1, string arg2): name(n), retval(ret), a1(arg1), a2(arg2) {} 
string getName() const { return name; }
string getRet() const { return retval; }
string getA1() const { return a1; }
string getA2() const { return a2; }
virtual void call() = 0; 

private: 
string name, retval, a1, a2; 
};  

template <typename ret, typename arg1, typename arg2> 
class Func: public FuncBase {
public:
Func(string n, string r, string ar1, string ar2, ret(*fc)(arg1, arg2)):
                FuncBase(n, r, ar1, ar2), func(fc) {}
void call() {
    arg1 ar1;
    arg2 ar2;
    cout << "You chose the " << getName() << " function" << endl; 
    cout << "Enter a " << getA1() << ": "; 
    cin  >> ar1;
    cout << "Enter a " << getA2() << ": "; 
    cin  >> ar2; 

    if (getRet() != "void") {
        ret val = (*func)(ar1, ar2);
        cout << getName() << " returned " << val << endl; 
    }
    else (*func)(ar1, ar2); 
    cout << endl; 
}

private:
// pointer to function
ret(*func)(arg1, arg2);
}; 

For template instantiations where this fails, func is a function pointer that receives two arguments and has void return type. 对于失败的模板实例化, func是一个函数指针,该函数指针接收两个参数并且具有void返回类型。 That is ret is void . 那是retvoid A function with a void return type does not return anything. 返回类型为void的函数不返回任何内容。 Naturally you cannot read the return value of an invocation of (*func)() because you said that there was no return value. 自然,您不能读取(*func)()的调用的返回值,因为您说没有返回值。

Even though the branch that reads the return value won't get executed, it still needs to compile and pass the static type checking. 即使读取返回值的分支不会执行,它仍然需要编译并通过静态类型检查。 The fundamental issue here is that the calls to *func() are dealt with at compile time and are subject compile time static type checking. 这里的根本问题是,对*func()的调用在编译时处理,并且是主题编译时静态类型检查。

This Stack Overflow question covers the exact same problem as you and the accepted answer shows how to deal with the issue 堆栈溢出问题涵盖了与您完全相同的问题,并且可接受的答案显示了如何处理该问题

I assume that 'arg1', 'arg2', and 'ret' are all template parameters. 我假设'arg1','arg2'和'ret'都是模板参数。 Given that, even if the runtime path the code will take you to (*func)(ar1, ar2) , the compiler still has to compile ret val = (*func)(ar1, ar2); 鉴于此,即使代码会将您带到运行时路径(*func)(ar1, ar2) ,编译器仍必须编译ret val = (*func)(ar1, ar2); , which makes no sense for functions that return void. ,对于返回void的函数没有意义。 I assume this is giving you the error. 我认为这是给你的错误。 Can you provide us with a bit more of your code? 您能否为我们提供更多代码?

Edit: Okay now that I see what you are trying to do, you'll need something like this to execute void and non-void functions, with different behavior for each: 编辑:好的,现在我知道了您要做什么,您将需要执行以下操作来执行void和non-void函数,每种函数的行为不同:

int foo(){ return 5; }
void bar(){}

struct executor
{
    template <typename T>
    void exec( T (*func)())
    {
        T ret = (*func)();
        cout << "Non-void function called, returned " << ret << endl;
    }

    void exec( void (*func)())
    {
        (*func)();
        cout << "void function called" << endl;
    }
};

int main()
{

    executor ex;
    ex.exec(foo);
    ex.exec(bar);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从“ void”转换为“ int” - cannot convert from “void” to “int” C2440无法从&#39;void(_cdecl *)(int)&#39;转换为&#39;void(_cdecl&)(int) - C2440 cannot convert from 'void (_cdecl*)(int)' to 'void(_cdecl&)(int) 无法将类型&#39;void(classname ::)(unsigned char,int,int)&#39;的&#39;classname :: glutKeyboard&#39;转换为类型&#39;void(*)(unsigned char,int,int)&#39;的类型 - cannot convert 'classname::glutKeyboard' from type 'void (classname::)(unsigned char, int, int)' to type 'void (*)(unsigned char, int, int)' 无法将参数 '2' 的 'int (Scheduler::*)(int, void*)' 转换为 'int (*)(int, void*)' 到 'bool irq_InstallISR(int, int (*)(int, void *), 空白*)' - cannot convert 'int (Scheduler::*)(int, void*)' to 'int (*)(int, void*)' for argument '2' to 'bool irq_InstallISR(int, int (*)(int, void*), void*)' 无法从char转换为void * - cannot convert from char to void* x = malloc(n * sizeof(int)); 无法将void转换为int - x = malloc(n * sizeof (int)); cannot convert void to int 无法从int [] []转换为int **? - cannot convert from int[][] to int**? &#39;=&#39;:无法从&#39;int(*)[5]&#39;转换为&#39;int *&#39; - '=': cannot convert from 'int (*)[5]' to 'int *' 无法从'int *'转换为'int []'? - Cannot convert from 'int *' to 'int []'? 无法从int [] []转换为int * - Cannot Convert from int[][] to int*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM