‘cout’ 没有命名类型
Posted
技术标签:
【中文标题】‘cout’ 没有命名类型【英文标题】:‘cout’ does not name a type 【发布时间】:2012-03-29 23:24:55 【问题描述】:我正在学习 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;
但是有一些错误:
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
我搜索了this,this,this 和this,但找不到答案。
任何帮助将不胜感激:)
【问题讨论】:
main()
在哪里?
你错过了你的主要。代码位于函数之外,编译器将其视为变量、类、结构或其他此类命令的声明。只需将所有底部代码放入 int main()
【参考方案1】:
问题在于您拥有的用于打印的代码不在任何功能范围内。不是 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;
【讨论】:
为什么打印应该只在功能中? 这是一个更一般规则的一部分,即只有声明语句才能出现在 C++ 中的函数之外。我不知道这是否有深刻的哲学原因,或者它是否是历史文物。 @templatetypedef 好的【参考方案2】:您的程序代码周围缺少函数声明。以下应该可以解决您的错误:
#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;
然后您得到的错误(类似于“从 int 到 char* 的无效转换”)是因为您尝试将整数值 (30) 设置为字符串属性(名称)
node2.name=30;
我认为
node2.age=30;
应该是正确的。
【讨论】:
【参考方案3】:main()
函数丢失。C++ 中应该有一个main()
函数,你应该把cout
放入一个函数中。
【讨论】:
【参考方案4】:如果你想在函数外使用 cout,你可以通过在 boolean 中收集 cout 返回的值来实现。见下面的例子
#include<iostream>
using namespace std;
bool b=cout<<"1";
int main()
return 0;
输出:
error prog.cpp:4:14: error: cannot convert 'std::basic_ostream<char>' to 'bool' in initialization
bool b=cout<<"1";
【讨论】:
【参考方案5】:包括:
int main()
//code
return 0;
会帮助你。这个问题经常出现在书本上学习的人身上,通常几章后就不用main函数了。
【讨论】:
【参考方案6】:对于类错误:你应该在类的任何方法中使用 cout。你不能在任何类中公开使用 cout。
class result:public exam
public:
display_result()
float percentage;
percentage=(physics + maths) / 2;
get_roll_number();
getmarks();
cout << "The Final Percentage of the Student is =" <<percentage<< "%" << endl;
;
【讨论】:
以上是关于‘cout’ 没有命名类型的主要内容,如果未能解决你的问题,请参考以下文章