不明确符号

刘德华不爱张学友 2010-01-06 12:03:51
#include<stdafx.h>
#include<iostream>
using namespace std;
#include<string.h>
class string{
public:
string(int size=80);
string(char *s);
string(string &s);
~string(){
delete sptr;
}
int getlength(){
return length;
}
void print(){
cout<<sptr<<endl;
}
private:
int length;
char *sptr;
};
string::string(int size){
length=size;
sptr=new char[length+1];
*sptr='\0';
}
string::string(char *s){
length=strlen(s);
sptr=new char[length+1];
strcpy(sptr,s);
}
string::string(string &s){
length=s.length;
sptr=new char[length+1];
strcpy(sptr,s.sptr);
}
void main(){
string s1,s2("This is a string.");
cout<<s1.getlength()<<endl;
s2.print();
char *str1="That is a program.";
string s3(str1);
s3.print();
string s4(s2);
s4.print();
}

《C++语言程序设计》吕风xi版例11.1在vs2008team版编译
编译出现:error C2872: “string”: 不明确的符号
请问应如何解决?
...全文
1469 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sevendog 2010-10-11
  • 打赏
  • 举报
回复
???????????????????????????????????????
brookmill 2010-01-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ejleo 的回复:]
我觉得可以不用加也行
这个例子只是为了说明类构造函数的重载

[/Quote]
这个例子不用加也行,只是一个编程习惯和规范问题。
比如,开车应该系安全带,但是实际上99%以上都没用。
brookmill 2010-01-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ejleo 的回复:]
自己找到问题了 string是保留关键字 不能用来作为类名 换个类名就欧拉
[/Quote]
string不是关键字
  • 打赏
  • 举报
回复
我觉得可以不用加也行
这个例子只是为了说明类构造函数的重载
brookmill 2010-01-06
  • 打赏
  • 举报
回复
其它:
#include <string.h> 标准写法 #include <cstring>
构造函数和拷贝构造函数的参数应该加上const
getlength和print也都应该加上const:
int getlength() const { return length; }
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 brookmill 的回复:]
自定义的string类和标准库的string重名了。
把所有的string都改成String,或者mystring,或者随便其它什么名字

[/Quote]
依然谢谢
  • 打赏
  • 举报
回复
自己找到问题了 string是保留关键字 不能用来作为类名 换个类名就欧拉
brookmill 2010-01-06
  • 打赏
  • 举报
回复
自定义的string类和标准库的string重名了。
把所有的string都改成String,或者mystring,或者随便其它什么名字

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧