首页
社区
课程
招聘
[求助]VC字符串类如何继承?
发表于: 2013-12-11 14:05 5152

[求助]VC字符串类如何继承?

2013-12-11 14:05
5152
在C中对字符数组可以通过atoi转成整数,但是C++中的string类就不能用atoi了。我就想编写一个相应的stoi。
我的方法是派生一个子类spstring,将stoi作为成员函数。H代码如下:
#include <cctype>
#include <string>
#include <algorithm>
#include <sstream>
#include <stdint.h>
#include <xstring>


class spstring : public std::string {
public:
	spstring(std::string s):std::string(s){}  //构造函数
	int stoi(); //声明转换函数
	spstring Spstring(std::string s); //声明强制类型转换函数
};

spstring spstring::Spstring(std::string s)
{
	spstring spstr(s); //强制类型转换
	return(spstr);
}


然后在main中调用
spstring byteaddstr(std::string(argv[1])); //把参数转为spstring型
int byteadd;
byteadd=byteaddstr.stoi(); //把spstring转为int


结果error C2228: “.stoi”的左边必须有类/结构/联合

百思不得其解,byteaddstr已经成功构造了,为何不能调用它的成员函数呢?

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 209
活跃值: (138)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
API函数StrToInt通吃加好用
2013-12-11 14:10
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
谢谢,不过我还是想知道我的方法为什么行不通。
2013-12-11 14:21
0
雪    币: 6
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
解决了,把
spstring byteaddstr(std::string(argv[1]))
改成
spstring byteaddstr((std::string(argv[1])))
即可,否则VC会把spstring byteaddstr(...)视作函数声明而非变量声明。
2013-12-11 16:06
0
游客
登录 | 注册 方可回帖
返回
//