'回复'未在此范围内声明 C++

发布于 2024-12-27 16:59:01 字数 628 浏览 0 评论 0原文

我知道很多人之前都遇到过这个错误,但我刚刚开始使用 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

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

谢谢你, 沙雷克斯

I knew that a lot of people got this error before, but I just started programming with C++, so I'm not really sure about most of the commands yet.

I am trying to create a program with the following source code:

#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;
}

Now when I tried to compile this with my compiler (G++). I received this error:

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

Can you please tell me what exactly went wrong with my code? And how should I attempts to solve it?

Thank you,
Xarlexus

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

护你周全 2025-01-03 16:59:01

如果添加(可选)大括号,情况会更清楚:

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;
}

It's a bit clearer if you add the (optional) braces:

if (input == "male")
{
    char reply[] = "Mr";
}
else
{
    char reply[] = "Mrs";
}

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

reply ceases to exist at the } that ends the block in which it is declared. So, here, reply does not exist when you try to print it.

The solution here is to declare reply outside of the blocks, then assign to it from within the blocks:

char const* reply(0);

if (input == "male")
{
    reply = "Mr";
}
else
{
    reply = "Mrs";
}

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

This way, reply is still in scope (and still exists) on the last line when you print it.


Note, however, that while your program may now compile, it is still incorrect. input == "male" does not do what you think: input and "male" become pointers to C strings and the pointers are compared, not the contents of the pointed-to strings. You need to use a string comparison function, or better yet, use std::string, which overloads == to have string comparison semantics.

A cleaner, correcter version of your program might look like so:

#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;
}
烟─花易冷 2025-01-03 16:59:01

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;
}

请注意,原始代码中的此代码段并未执行您所期望的操作:if (input == "male")。当您打算比较字符串时,您正在比较指针。最好使用 std::string 甚至 strcmp

reply exists locally within the scope of the if/else blocks. To access it outside of them, you must declare it outside of that scope.

#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;
}

Note that this snippet in your original code doesn't do what you expect: if (input == "male"). You're comparing pointers, when you intend to compare strings. Preferably use std::string or even strcmp.

离不开的别离 2025-01-03 16:59:01

每个 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[] 。

There is an implicit block in each if/else statement so your code reads:

if (input == "male") {
   char reply[] = "Mr";
} else {
   char reply[] = "Mrs";
}

//  reply is not defined in this scope

What you really want is something that's more like this:

const char *reply;

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

Though I would personally write it like this:

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

Also note that I would use const char * instead of the char [] since I assume that the typical use is as an immutable string rather than a char[] which is mutable.

银河中√捞星星 2025-01-03 16:59:01

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;
}

input == "male is a meaningless comparision, since it will never be true. input is an array, and will decay to a pointer to the first element. From there on, it's pointer comparision (i.e., are input and "male" at the same address?), which just will not be true.

You want a std::string instead:

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文