一个严重问题

tangcx 2003-09-13 11:49:30
#include <iostream>
#include <string>
using namespace std;
void main()
{ int len;
sting str="chinese";
len=size(str);
cout<<len;
}
编译的错误提示是:
--------------------Configuration: first - Win32 Debug--------------------
Compiling...
first use.cpp
F:\C++\first\first use.cpp(6) : error C2065: 'sting' : undeclared identifier
F:\C++\first\first use.cpp(6) : error C2146: syntax error : missing ';' before identifier 'str'
F:\C++\first\first use.cpp(6) : error C2065: 'str' : undeclared identifier
F:\C++\first\first use.cpp(6) : error C2440: '=' : cannot convert from 'char [8]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
F:\C++\first\first use.cpp(8) : error C2065: 'size' : undeclared identifier
Error executing cl.exe.

first use.obj - 5 error(s), 0 warning(s)
是不是 sting 定义错误,我是按书稍加修改的,
请问是什么问题呢?
...全文
30 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangcx 2003-09-13
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;
void main()
{ int len;
string str="chinese";

len=str.size();

cout<<"len="<<len<<endl<<str<<endl;


}

这样len=7
而len=sizeof(str)时,len=17,为什么这样呢
tangcx 2003-09-13
  • 打赏
  • 举报
回复
谢谢大家,我还以为是我的编译器有问题
RookieStar 2003-09-13
  • 打赏
  • 举报
回复
补充:len=str.size();
RookieStar 2003-09-13
  • 打赏
  • 举报
回复
是string 不是sting 一切OK!

lionhu2003 2003-09-13
  • 打赏
  • 举报
回复
有两个错误
正确的如下

#include <iostream>
#include <string>
using namespace std;
void main()
{ int len;
string str="chinese";不是sting
len=sizeof(str); 是sizeof(str)
cout<<len;
}

jxbicestare 2003-09-13
  • 打赏
  • 举报
回复
什么严重问题?自己马虎!
RookieStar 2003-09-13
  • 打赏
  • 举报
回复
sizeof求的是一个string对象所占用的字节数,我用的vc.net显示28;
string的size()操作求的才是一个string对象长度。

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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