C++的复杂问题

来源:百度知道 编辑:UC知道 时间:2024/05/24 00:47:07
请教。。
int cnt=0;
const int sz=cnt ;
这样写合法吗??

还有个小问题。。(以下程序,我吧#include <string>去掉以后,为什么string s1就会出现错误呢)
#include <iostream>
#include <string>
string s1="hello";
int main()
{
string s2="world";
cout << s1 << " " << s2 << endl;
return 0;
}

第一个是合法的.
因为我估计你用的是非国际标准的VC++6.0所以是要加#include <string>的,但你使用BCB,Dev-C++都可以没有#include <string>.

请教。。
int cnt=0;
const int sz=cnt ;
这样写合法吗??

答:不合法,因为,int cnt是变量,它的值是在运行时确定的,const int sz是常量,它的值是在编译期确定的.

-----------------------------------------------------------------------
还有个小问题。。(以下程序,我吧#include <string>去掉以后,为什么string s1就会出现错误呢)

答:因为VC++6.0没有预先包含这些头文件.

#include <iostream>
#include <string>
string s1="hello";
int main()
{
string s2="world";
cout << s1 << " " << s2 << endl;
return 0;
}