'cout'不是一个类型 [英] ‘cout’ does not name a type
本文介绍了'cout'不是一个类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习Adam Drozdek的书C ++中的数据结构和算法,我在vim中输入了第15页的代码,并将其编译到Ubuntu 11.10的终端。
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.
#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;
但有一些错误:
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,this and this, but I can't find the answer.
)
推荐答案
我认为问题是,你有打印的代码是在任何函数之外。 C ++中的语句需要在函数内部。例如:
I think 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:
#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;
}
希望这有助!
这篇关于'cout'不是一个类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文