| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3771 人关注过本帖
标题:c++中用strcpy和strcat报错
只看楼主 加入收藏
知秋一叶
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-3-11
收藏
得分:0 

关于名字空间的使用请参见:
《C++ Primer》----8.6 使用名字空间成员-----P631
如果不用using namepace std,还有其他的两种选择。下面是其中之一:

#include <iostream.h>
#include <string.h>

using std::string;
using std::cout;
using std::cin;
using std::endl;

class MyClass{
string str;
public:
MyClass(){}
MyClass(const std::string& str_):str(str_){}

void input(){
cout<<"please input a string: ";
cin>>str;
}

void print() const{
cout<<"The result: ";
cout<<str<<endl;
}

friend MyClass operator+(const MyClass &a,const MyClass &b){
return MyClass(a.str+' '+b.str);
}

~MyClass(){}
};

int main(){
MyClass a,b,c;
a.input();
b.input();

c=a+b;
c.print();

return 0;
}

2006-12-26 23:56
forever043
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-10-15
收藏
得分:0 
<string.h> 和 <string> 不是同一个文件,
<string>里面是关于string类的定义,
<string.h>里是C中的字符串处理函数,
可以打开include文件夹自己看一看

学操作系统真的好郁闷啊.........
2006-12-27 08:54
一二三四五
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:856
专家分:0
注 册:2006-11-13
收藏
得分:0 
学习了


hey,di va la
2006-12-27 11:24
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
得分:0 

受教了,谢谢各位!!!!!


C斗士~~~fighting!!!!
2007-01-01 09:01
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
如果用C++ 的string 类, 下面的两种写法是有区别的。
写法1: cin>>str;
写法2: getline(cin, str);

前者你只能得到一个单词,比如你输入hello world, 那么只有hello 被变量str 接受了。
后者你能够得到完整的输入,如果输入hello world, 那么str 的内容就是hello world

还有就是12楼的观点完全正确,这也是往往会搞混淆的地方。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2007-01-01 18:24
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

还真没注意这么多,学习了,谢谢!

[此贴子已经被作者于2007-1-1 23:57:42编辑过]


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-01-01 23:35
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
得分:0 

学习了


C斗士~~~fighting!!!!
2007-01-02 17:00
快速回复:c++中用strcpy和strcat报错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018149 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved