'回复'未在此范围内声明 C++
我知道很多人之前都遇到过这个错误,但我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果添加(可选)大括号,情况会更清楚:
reply
在声明它的块结束的}
处不再存在。因此,当您尝试打印时,reply
并不存在。这里的解决方案是在块之外声明
reply
,然后从块内分配给它:这样,
reply
仍然在范围内(并且仍然存在)打印时的最后一行。但请注意,虽然您的程序现在可以编译,但它仍然不正确。
input == "male"
并不符合您的想法:input
和"male"
成为指向 C 字符串的指针,并对指针进行比较,不是指向字符串的内容。您需要使用字符串比较函数,或者更好的是使用std::string
,它重载==
以具有字符串比较语义。程序的更清晰、更正确的版本可能如下所示:
It's a bit clearer if you add the (optional) braces:
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: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, usestd::string
, which overloads==
to have string comparison semantics.A cleaner, correcter version of your program might look like so:
reply
本地存在于 if/else 块的范围内。要在它们之外访问它,您必须在该范围之外声明它。请注意,原始代码中的此代码段并未执行您所期望的操作:
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.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 usestd::string
or evenstrcmp
.每个 if/else 语句中有一个隐式块,因此您的代码如下所示:
您真正想要的是更像这样的东西:
虽然我个人会这样写:
另请注意,我会使用
const char * 而不是 char [] ,因为我假设典型用途是作为不可变字符串而不是可变的 char[] 。
There is an implicit block in each if/else statement so your code reads:
What you really want is something that's more like this:
Though I would personally write it like this:
Also note that I would use
const char *
instead of thechar []
since I assume that the typical use is as an immutable string rather than a char[] which is mutable.input == "male
是一个毫无意义的比较,因为它永远不会成立。input
是一个数组,并且会衰减为一个指针从那里开始,它是指针比较(即,input
和"male"
位于同一地址?),这不是您想要 的。改为
std::string
: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., areinput
and"male"
at the same address?), which just will not be true.You want a
std::string
instead: