C++ 'cout' 没有命名类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9935027/
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
‘cout’ does not name a type
提问by pvd
I was learning Adam Drozdek's book "Data Structures and Algorithms in C++", well, I typed the code in page 15 in my vim and compiled it in terminal of my Ubuntu 11.10.
我正在学习 Adam Drozdek 的书“C++ 中的数据结构和算法”,好吧,我在 vim 中输入了第 15 页中的代码,并在我的 Ubuntu 11.10 的终端中编译了它。
#include <iostream>
#include <cstring>
using namespace std;
struct Node{
char *name;
int age;
Node(char *n = "", int a = 0){
name = new char[strlen(n) + 1];
strcpy(name, n);
age = a;
}
};
Node node1("Roger", 20), node2(node1);
cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
strcpy(node2.name, "Wendy");
node2.name = 30;
cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
But there's some error:
但是有一些错误:
oo@oo:~$ g++ unproper.cpp -o unproper
unproper.cpp:15:23: warning: deprecated conversion from string constant to ‘char*' [-Wwrite-strings]
unproper.cpp:16:1: error: ‘cout' does not name a type
unproper.cpp:17:7: error: expected constructor, destructor, or type conversion before ‘(' token
unproper.cpp:18:1: error: ‘node2' does not name a type
unproper.cpp:19:1: error: ‘cout' does not name a type
I have searched this,this,thisand this, but I can't find the answer.
Any help would be appreciated:)
任何帮助,将不胜感激:)
回答by templatetypedef
The problem is that the code you have that does the printing is outside of any function. Statements in C++ need to be inside a function. For example:
问题是您拥有的用于打印的代码在任何函数之外。C++ 中的语句需要在函数内部。例如:
#include <iostream>
#include <cstring>
using namespace std;
struct Node{
char *name;
int age;
Node(char *n = "", int a = 0){
name = new char[strlen(n) + 1];
strcpy(name, n);
age = a;
}
};
int main() {
Node node1("Roger", 20), node2(node1);
cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
strcpy(node2.name, "Wendy");
node2.name = 30;
cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
}
回答by Heinzi
You are missing the function declaration around your program code. The following should solve your error:
您缺少程序代码周围的函数声明。以下应该可以解决您的错误:
#include <iostream>
#include <cstring>
using namespace std;
struct Node{
char *name;
int age;
Node(char *n = "", int a = 0){
name = new char[strlen(n) + 1];
strcpy(name, n);
age = a;
}
};
int main()
{
Node node1("Roger", 20), node2(node1);
cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
strcpy(node2.name, "Wendy");
node2.name = 30;
cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
}
The error you then get (something like "invalid conversion from int to char*") is because you try to set an integer value (30) to a string attribute (name) with
然后您得到的错误(类似于“从 int 到 char* 的无效转换”)是因为您尝试将整数值 (30) 设置为字符串属性(名称)
node2.name=30;
I think
我认为
node2.age=30;
would be correct.
会是正确的。
回答by Eric
main()
function is missed.There should be a main()
function in C++ ,and you should put cout
into a function.
main()
函数丢失main()
了。C++中应该有一个函数,你应该放入cout
一个函数中。
回答by Gaurav Kaushik
If you want to use cout outside the function you can do it by collecting the value returned by cout in boolean.see the below example
如果你想在函数外使用 cout,你可以通过收集 cout 返回的布尔值来实现。参见下面的例子
#include<iostream>
using namespace std;
bool b=cout<<"1";
int main()
{
return 0;
}
output:
输出:
error prog.cpp:4:14: error: cannot convert 'std::basic_ostream<char>' to 'bool' in initialization
bool b=cout<<"1";
回答by Kapil
Including:
包含:
int main()
{ //code
return 0;
}
will help you. This problem usually occurs to those who are learning from book in which they usually don't use main function after a few chapters.
会帮助你。这个问题通常发生在那些从书本中学习的人身上,他们通常在几章之后就不会使用 main 函数。