没有包含string的后果
来源:互联网 发布:淘宝首页找不到购物车 编辑:程序博客网 时间:2024/04/29 20:45
写了一个很简单的程序:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string user_name;
cout << "Please enter your name:";
cin >> user_name;
cout << "/nHello, " << user_name << " ... and goodbye!/n";
return 0;
}
注释掉string库时,产生了如下的一些错误:
e:/cppprogram/hello.cpp/hello.cpp.cpp(14) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
e:/cppprogram/hello.cpp/hello.cpp.cpp(15) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
在MSDN上找了一下error 2679,但没结果!另外这点说明'<<'操作符的一些什么问题呢?
接着将using namespace std;也注释掉,结果更是大吃一惊:
E:/CPPProgram/hello.cpp/hello.cpp.cpp(8) : error C2065: 'string' : undeclared identifier
E:/CPPProgram/hello.cpp/hello.cpp.cpp(8) : error C2146: syntax error : missing ';' before identifier 'user_name'
E:/CPPProgram/hello.cpp/hello.cpp.cpp(8) : error C2065: 'user_name' : undeclared identifier
E:/CPPProgram/hello.cpp/hello.cpp.cpp(10) : error C2065: 'cout' : undeclared identifier
E:/CPPProgram/hello.cpp/hello.cpp.cpp(10) : error C2297: '<<' : illegal, right operand has type 'char [24]'
E:/CPPProgram/hello.cpp/hello.cpp.cpp(11) : error C2065: 'cin' : undeclared identifier
E:/CPPProgram/hello.cpp/hello.cpp.cpp(11) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
E:/CPPProgram/hello.cpp/hello.cpp.cpp(12) : error C2297: '<<' : illegal, right operand has type 'char [9]'
其实对于namespace,一直心存疑问:他是不是就是一段内存块的标志呢?
又将main 改为my_main后,更是大吃两惊了——编译竟然没错误!进而连接已编译程序,错误出现了:
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/hello.cpp.exe : fatal error LNK1120: 1 unresolved externals
这让我想到了C++程序的编译模式,它是先编译后连接!在编译期间,编译器以为此程序只是一个模块而已,因此对改变无动于衷!然而连接器就不同意了,它无法找到程序的入口——错误也因此应运而生!
对于以上的错误和警告,我得一一搞清楚才是!
================================================
看了一些书,对以上的问题有所了解了:
原来,C++中的输出操作符“<<”和输入操作符“>>”是要重载的!不同的类需要重载这些操作符,包括:"=", ">", "<"…………
如果重载的是输出操作符<<,第1个参数是对流ostream类的引用,第2个参数是对要定义的输入/输出类的引用,当然,也可以是该类的指针。
- 没有包含string的后果
- 没有注释的后果
- 没有北京户口的后果
- 没有配置localhost的后果
- 由于没有关闭connection,造成的后果
- GetDC()没有释放造成的后果
- 不包含全头文件的后果相当严重
- 少包含了一个头文件的后果
- 当没有给字符串留'\0'的位置的后果
- 内存没有初始化和内存越界的后果
- 有时候,没有首先执行super.OnCreate的后果很严重!
- String包含的方法
- 集合的初始尝试使用之登录&注册小程序-----没有重写equles方法的后果
- 10g升级11g没有运行utlu112i.sql的后果
- 当数据类型为long的变量后面没有L加会有什么后果
- 当数据类型为long的变量后面没有L加会有什么后果
- 关于在Eclipse新建项目时,没有勾选generate web.xml的后果
- controller返回实体类对象但是没有转化为json的后果
- 今年冬天不会冷
- String与StringBuffer -- 其实用String也不错
- (转贴)DotNet框架编程 读书笔记
- Control Study -> 自定义DataGrid翻页按钮(示例代码下载)
- 郁闷acm
- 没有包含string的后果
- 回合肥了
- 让Ant帮你武装到牙齿
- 第三章 语义“陷阱”
- 金日隧道广告系统的几个截图
- 我的开源.NET函数式编程类库——VBF 1.0 BETA1发布
- VS2005新建项目找不到工程模版错误的解决方法。
- ミスチルのアルバム『I♥U』
- JavaMail类