简体   繁体   English

C ++我是否错误地使用了void函数?

[英]C++ Am I using void functions incorrectly?

Errors: 错误:

  • C2182: 'tellStats' : illegal use of type 'void' C2182:'tellStats':非法使用'void'类型
  • C2440: 'initializing' : cannot convert from 'std::string' to 'int' C2440:'初始化':无法从'std :: string'转换为'int'
  • incomplete type is not allowed. 不允许不完整的类型。

All of these errors are found on this line: 所有这些错误都可以在这一行找到:

    //ClassPractice.cpp
    void tellStats(pick);

which calls... 哪个叫......

    //Functions.h
    void tellStats(string);

which is defined as... 这被定义为......

    //Functions.cpp
        void tellStats(string choice)
    {
        if (choice == "wizard")
            {
            cout << "These are the Wizard's stats:" << endl;
            cout << "Max HP: 80\nSpeed: 7\nAttack: 10" << endl;
            }
    }

I don't understand why I am getting these errors. 我不明白为什么我会收到这些错误。 I don't know why an int is even involved with the error. 我不知道为什么int甚至涉及错误。 I see nothing referring to ints in these sections of code. 在这些代码部分中,我没有看到任何涉及int的内容。 I thought I was using 'void' correctly because I don't want to return a value with the function. 我以为我正确使用'void'因为我不想用函数返回值。

You dont call a function with the return type. 你不要用返回类型调用函数。 Dont say: 不要说:

void tellStats(pick);

just use tellStats(pick); 只需使用tellStats(pick); .

Is that line the only place you call tellStats? 那条线路是你调用tellStats的唯一地方吗?

The int is a red herring. int是红鲱鱼。 For historical reasons, compilers internally often substitute int when they're missing a type. 由于历史原因,编译器内部通常在缺少类型时替换int This normally shouldn't happen, but here you see this exposed because the compiler tried to continue after the first error. 这通常不应该发生,但在这里你看到这暴露,因为编译器试图在第一个错误后继续。 The compiler incorrectly guessed that you wanted to define a variable tellStats of type void int and initialize that variable with a string. 编译器错误地猜测您要定义 void int类型的变量tellStats并使用字符串初始化该变量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM