【问题标题】:'reply' was not declared in this scope C++'reply' 未在此范围 C++ 中声明
【发布时间】:2012-02-12 20:58:46
【问题描述】:

我知道很多人之前都遇到过这个错误,但我刚开始用 C++ 编程,所以我还不确定大多数命令。

我正在尝试使用以下源代码创建程序:

#include <iostream>

int main()
{
char input[7];

std::cout << "Enter your gender (male or female):";
std::cin.getline (input, 6);

if (input == "male")
char reply[] = "Mr";

else
char reply[] = "Mrs";

std::cout << "Hello " << reply << "!\n";

return 0;
}

现在,当我尝试使用我的编译器 (G++) 编译它时。我收到了这个错误:

StringTest.cpp: In function 'int main()':
StringTest.cpp: 16:26: error: 'reply' was not declared in this scope

您能告诉我我的代码到底出了什么问题吗?我应该如何尝试解决它?

谢谢你, Xarlexus

【问题讨论】:

    标签: c++ string if-statement scope compiler-errors


    【解决方案1】:

    加上(可选的)大括号会更清楚一点:

    if (input == "male")
    {
        char reply[] = "Mr";
    }
    else
    {
        char reply[] = "Mrs";
    }
    
    std::cout << "Hello " << reply << "!\n";
    

    reply 在结束声明它的块的} 处不再存在。所以,在这里,reply 在您尝试打印时并不存在。

    这里的解决方案是在块外声明reply,然后从块内分配给它:

    char const* reply(0);
    
    if (input == "male")
    {
        reply = "Mr";
    }
    else
    {
        reply = "Mrs";
    }
    
    std::cout << "Hello " << reply << "!\n";
    

    这样,reply 在您打印时仍在最后一行的范围内(并且仍然存在)。


    但是请注意,虽然您的程序现在可以编译,但它仍然不正确。 input == "male" 没有按照你的想法做:input"male" 成为指向 C 字符串的指针,并且比较指针,而不是指向字符串的内容。您需要使用字符串比较函数,或者更好的是使用std::string,它重载== 以获得字符串比较语义。

    您的程序的更简洁、更正确的版本可能如下所示:

    #include <iostream>
    
    int main()
    {
        std::string input;
    
        std::cout << "Enter your gender (male or female):" << std::endl;
    
        if (!std::getline(std::cin, input))
        {
            std::cout << "Oops, something bad happened during input!" << std::endl;
            return 0;
        }
    
        std::string reply;
        if (input == "male")
        {
            reply = "Mr";
        }
        else if (input == "female")
        {
            reply = "Mrs";
        }
        else
        {
            std::cout << "Your selection was invalid" << std::endl;
            return 0;
        }
    
        std::cout << "Hello " << reply << "!" << std::endl;
        return 0;
    }
    

    【讨论】:

    • @Xarlexus:你不能,至少不能直接。 reply 的类型为 const char[3]const char[4];它不能同时是两者。您可以将其设为const char* 而不是数组(这意味着sizeof reply 为您提供指针的大小,而不是数组的大小)。或者,由于您使用 C++ 进行编程,您可以使用 std::string,它更加健壮和灵活。
    • @KeithThompson,嗯,我想我现在明白如何回复不能同时是 const char[3] 和 const char[4]。感谢您的回复。
    【解决方案2】:

    reply 本地存在于 if/else 块的范围内。要在它们之外访问它,您必须在该范围之外声明它。

    #include <iostream>
    #include <string>
    int main()
    {
      std::string input;
    
      std::cout << "Enter your gender (male or female):";
      stdgetline(cin, input);
    
      std::string reply;
      if (input == "male") {
        reply = "Mr";
      }
      else {
        reply = "Mrs";
      }
      std::cout << "Hello " << reply << "!\n";
    
      return 0;
    }
    

    请注意,原始代码中的这个 sn-p 并没有达到您的预期:if (input == "male")。当您打算比较字符串时,您正在比较指针。最好使用std::string 甚至strcmp

    【讨论】:

      【解决方案3】:

      每个 if/else 语句中都有一个隐式块,因此您的代码如下:

      if (input == "male") {
         char reply[] = "Mr";
      } else {
         char reply[] = "Mrs";
      }
      
      //  reply is not defined in this scope
      

      你真正想要的是更像这样的东西:

      const char *reply;
      
      if (input == "male")
        reply = "Mr";
      else 
        reply = "Mrs";
      

      虽然我个人会这样写:

      const char * reply = (input == "male") ? "Mr" : "Mrs";
      

      另外请注意,我会使用 const char * 而不是 char [],因为我假设典型的使用是作为不可变字符串而不是可变的 char[]。

      【讨论】:

        【解决方案4】:

        input == "male 是一个毫无意义的比较,因为它永远不会是真的。 input 是一个数组,将衰减为指向第一个元素的 指针。从那里开始,它的指针比较(即,input"male" 在同一个地址?),这不会是真的。

        你想要一个 std::string 代替:

        #include <iostream>
        #include <string> // <== add this
        
        int main()
        {
          std::string input; // <== change type of 'input'
        
          std::cout << "Enter your gender (male or female):";
          std::getline(std::cout, input); // <== use free function version
        
          char const* reply;
          if (input == "male") { // <== now does the correct comparision
            reply = "Mr";
          }
          else {
            reply = "Mrs";
          }
          std::cout << "Hello " << reply << "!\n";
        
          return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-09-29
          • 2010-12-17
          • 1970-01-01
          • 2021-07-17
          • 2015-08-21
          • 2015-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多