为什么是“cout”?功能不起作用? [英] why is the "cout" function not working in functions?
问题描述
大家好,
我是初学者,对代码有一个小问题。
编译器标出错误说: -
hello everyone,
i'm a beginner and have a small problem regarding a code.
the compiler flags an error saying:-
error C2065: 'cout' : undeclared identifier
这里是代码
here is the code
char downer(char[13])
{
cout<<"hello";
}
using namespace std;
int main()
{
char ocean[13];
puts(" please enter the name of the ocean ");
cin>>ocean;
downer(ocean);
}
请帮我处理这个错误。
这里是一个指向整个代码的链接(以防万一你需要改写它)
链接 [ ]
推荐答案
问题与无关函数。
您应该使用#include< iostream>
以及使用命名空间std
:
The problem has nothing to do with "functions".
You should use#include <iostream>
and alsousing namespace std
:
#include <iostream>
using namespace std;
//...
void SomeFunction(/* ... */)
{
cout << "Now working!";
//...
}
//...
您还需要了解命名空间的工作原理,以及可能还有很多。
我可以看到问题的变化。不过,它需要修复。该代码缺少 #include< iostream>
。现在,代码将无法编译,因为 downer
不会返回任何内容。因此,将返回类型更改为 void
。并且该函数的参数毫无意义,将其从函数定义和调用中删除。 (将来,您可能需要 char *
参数,以传递不指定其长度的字符串;通常,在C和C ++中,使用以null结尾的字符串。)
这就是全部。
正如Stefan_Lang指出的那样,不使用使用命名空间有很多好处。您可以使用完全限定名称。这种方式排除了名称冲突,并且 - 在这种情况下非常重要 - 促进了解真实情况。
上面显示的输出行看起来像
You also need to learn how namespaces work, and probably a lot more.
I can see the changes in the question. Still, it need fixes. The code lacks #include <iostream>
. Now, the code won't compile, because downer
does not return anything. So, change return type to void
. And parameter of this function is pointless, removed it from function definition and the call. (In future, you may need char*
parameter, to pass a string without specification of its length; usually, in C and C++, null-terminated string is used.)
That's all.
As Stefan_Lang pointed out, there is are considerable benefits of not using "using namespace". You can use fully-qualified names instead. This way excludes name clash, and — very important in this very case — promotes understanding of what really goes on.
The output line shown above will look like
std::cout << "Working without using namespace";
祝你好运,
我已经重写了你的代码,所以你可以看到你的错误。
在此之前,由于我认为,根据您的代码,您熟悉C语言的一小部分解释,因此这些解释可能对您有所帮助:
让我们从你的功能开始吧。
既然你只想在控制台上写点东西,使用cout,你不需要返回值(参见代码中的注释,char在void中更改)。
此外,我已将cout语句更改为输出您传递给函数的海洋字符串,请参阅代码。
您已经以错误的方式将字符串传递给函数downer,我已经更改了也好。
忘掉看跌期权,改用cout,我已经改变了也是代码。
我使用double作为带小数点的变量的类型,而不是浮点数,请参阅代码。
我在代码的最后添加了return语句,这表示main退出时没有错误。
在我看来你知道C并尝试学习C ++。我希望这段代码可以帮到你,如果你需要发表评论,我会帮助你。
最后,代码:
I have rewritten your code, so you can see your mistakes.
Before that, a small set of explanations, since I believe, based on your code, that you are familiar with C, so these explanations might help you:
Let us start with your function.
Since you just want to write something to the console, using cout, you do not need the return value ( see the comments in the code, char is changed in void ).
Also, I have changed cout statement to output the ocean string, that you have passed to the function, see the code.
You have passed the string to the function downer in a wrong way, I have changed that too.
Forget about puts, use cout instead, I have changed that in my code too.
Instead of float, I have used double as a type for your variables with decimal points, see the code.
I have added return statement at the very end of the code, this signals that main exited without error.
It seems to me that you know C and try to learn C++. I hope that this code will help you, if you need anything just post a comment, and I will help you.
Finally, the code:
#include<iostream>
using namespace std;
void downer( char* test) // <-- this is how you pass a string to a function
{
// endl just tells cout to go to new line, it is like '\n' in C
cout<<"hello kaka" << endl << test<< endl;
}
int main()
{
char ocean[13];
int boats,fiiden;
// use double instead of float for decimal numbers
double poden=11.29387,inden=12.989,atden=10.28172,arden=9.192772;
cout << " this program calculates the number of fish in any specified ocean " << endl;
// no need for puts, use cout instead
cout << " please enter the name of the ocean " << endl;
cin >> ocean;
downer( ocean );
return 0; // this indicates that main exited successfully without errors!
}
因为您忘记包含头文件...
尝试:
Because you forgot to include the header file...
Try:
#include <iostream>
这篇关于为什么是“cout”?功能不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!