帮小弟我看看这个程序威慑呢么不能运行,多谢
帮我看看这个程序威慑呢么不能运行,谢谢
#include<iostream.h>
#include<string.h>
ostream& operator<<(ostream& cout,char *string)
{
char *str=strupr(string);
while(*str)
{
cout.put(*str);
*str++;
}
return cout;
}
void main(void)
{
cout<<"This is test";
cout<<"Jamsa's c Programmer's Bible";
}
------解决方案--------------------
#include<iostream.h>
iostream是c++中的库文件不是C中的,C++中的库文件不要使用.h后缀,至少在vs2008编译器下是无法通过编译的
cout<<"This is test";
cout是命名空间std中的对象,你没有使用命名空间而直接使用cout这个对象,会发生编译错误,
ostream& operator<<(ostream& cout,char *string)
在这个函数的参数列表中,使用了cout,最好不要使用和命名空间中相同的名字,否则容易发生冲突,并且有时候可能不知道使用的是自己定义的还是标准库中的。
*str++;这句没有错,只是浪费了从str中取的值,因为你取*str的值没有在其他地方使用,浪费,只使用str++就可以了,如果仅仅是为了自增而没有其他用处,最好使用前缀的++str;后缀的++效率没有前缀的高。
你的代码修改如下:
#include<iostream.h>
#include<string.h>
ostream& operator<<(ostream& cout,char *string)
{
char *str=strupr(string);
while(*str)
{
cout.put(*str);
*str++;
}
return cout;
}
void main(void)
{
cout<<"This is test";
cout<<"Jamsa's c Programmer's Bible";
}
------解决方案--------------------
#include<iostream.h>
iostream是c++中的库文件不是C中的,C++中的库文件不要使用.h后缀,至少在vs2008编译器下是无法通过编译的
cout<<"This is test";
cout是命名空间std中的对象,你没有使用命名空间而直接使用cout这个对象,会发生编译错误,
ostream& operator<<(ostream& cout,char *string)
在这个函数的参数列表中,使用了cout,最好不要使用和命名空间中相同的名字,否则容易发生冲突,并且有时候可能不知道使用的是自己定义的还是标准库中的。
*str++;这句没有错,只是浪费了从str中取的值,因为你取*str的值没有在其他地方使用,浪费,只使用str++就可以了,如果仅仅是为了自增而没有其他用处,最好使用前缀的++str;后缀的++效率没有前缀的高。
你的代码修改如下:
- C/C++ code
#include<iostream> #include<string.h> using namespace std; ostream& operator<<(ostream& out,char *string) { char *str=strupr(string); while(*str) { out.put(*str++); } return out; } int main() { cout<<"This is test"; cout<<"Jamsa's c Programmer's Bible"; return 0; }