C++ sprintf 未在范围内声明?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13558159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sprintf not declared in scope?
提问by soniccool
I have a small snippet of code here from something i designed but i keep getting the error sprintf not declared in scope?
我在这里有一小段代码来自我设计的东西,但我不断收到错误 sprintf 未在范围内声明?
Do i include something in the #includes or how can i get this working? I was working on it on VS at my moms but came home and i cant get it on codeblocks
我是否在#includes 中包含了一些东西,或者我怎样才能让它工作?我在我妈妈的 VS 上工作,但回家后我无法在代码块上得到它
if (tmp2 <= B_dest[hr - 6])
{
sprintf(name, "B%d", tmp3);
}else{
sprintf(name, "A%d", tmp3);
}
回答by Alok Save
You need to include stdio.h.
您需要包括stdio.h.
#include<stdio.h>
The stdio.hdeclares the function sprintf, Without the header the compiler has no way of understand what sprintfmeans and hence it gives you the error.
该stdio.h声明的功能sprintf,如果没有头编译器无法理解的方式sprintf手段,因此它给你的错误。
In C++ Note that,
在 C++ 中注意,
Including cstdioimports the symbol names in stdnamespace and possiblyin Global namespace.
Including stdio.himports the symbol names in Global namespace and possiblyin stdnamespace.
包括cstdio在std命名空间和可能在全局命名空间中导入符号名称。
包括stdio.h在全局命名空间和可能的std命名空间中导入符号名称。
The same applies for all c-styled headers.
这同样适用于所有 c 风格的标题。
回答by srbhkmr
Make sure you've #include <cstdio>
确保你已经 #include <cstdio>
and access sprintf as std::sprintf()as pointed by @Potatoswatter.
并按照std::sprintf()@Potatoswatter 的指示访问 sprintf 。
or do the old c-style: #include <stdio.h>to include the definition of sprintf.
或者做旧的 c 风格:#include <stdio.h>包括sprintf的定义。
回答by serup
I had similar problem with C::B and found that the problem is more than just compiler paths - it seems that the IDE itself had problems opening #include <...> files -- this however could be solved by Settings -> Editor -> Other settings -> use encoding when opening files : default
我在 C::B 上遇到了类似的问题,发现问题不仅仅是编译器路径——似乎 IDE 本身在打开 #include <...> 文件时有问题——但这可以通过设置 -> 编辑器解决-> 其他设置 -> 打开文件时使用编码:默认
my encoding was not on default, and this somehow caused problems for the IDE to open include <...>
我的编码不是默认的,这以某种方式导致 IDE 打开 include <...>
It however did NOT solve the problem with "was not declared in this scope"
然而,它没有解决“未在此范围内声明”的问题

