首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

String str; 和 String str() ; 有什么区别?解决思路

2012-02-20 
String str 和 String str() 有什么区别?classString{public:String(){cout Constructor. endl

String str; 和 String str() ; 有什么区别?
class   String
{
public:
String(){   cout   < <   "Constructor. "   < <   endl   ;   }
~String()   {   cout   < <   "Destroyer. "       < <   endl   ;}
void   Print(){   cout   < <   "Print   sth. "       < <   endl   ;}
}   ;

int   main()
{
String   str1   ;//   语句1
String   str2()   ;//   语句2,这条语句是什么语句,好像一点不起作用?
}

//   但是编译器也没有说   语句2   错误!
---------
执行结果:
Constructor.
Destroyer.
Press   any   key   to   continue

====================编译环境:VC6.0==============

[解决办法]
String str1 ;// 定义一个string对象str1。
String str2() ; // 声明一个函数str2,没有参数,返回值为String类型。
[解决办法]
当定义对象时,如果不需要构造参数,就不要用扩号,因为编译器在区分不开是对象定义还是函数声明时会优先作为函数声明来考虑。

如果有参数就无所谓了,因为带参数的对象定义和带参数的函数声明语法上有很大的区别,编译器不会认不出来。
[解决办法]
在main里面都可以函数声明啊?这是我第一次听说哦。学习了。

这是在VS6.0中编译的结果,lz看一下吧。

------ 已启动生成: 项目: test, 配置: Release Win32 ------
正在编译...
test.cpp
.\test.cpp(16) : warning C4930: “String str2(void)”: 未调用原型函数(是有意用变量定义的吗?)
正在链接...
正在嵌入清单...
生成日志保存在“file://c:\Documents and Settings\zhuhao\My Documents\Visual Studio 2005\Projects\test\test\Release\BuildLog.htm”
test - 0 个错误,1 个警告
========== 生成: 1 已成功, 0 已失败, 0 最新, 0 已跳过 ==========

热点排行