error C2501: 'string' : missing storage-class or type specifiers
2007-05-17 17:50
453 查看
1.首先正确包含头文件
使用下面语句
#include <string>
没有.h,
否则无法引用标准模板库
2.使用命名空间
即使用下面语句:
using namespace std;
3.string 所创建的对象不能够直接使用里面的字符串
如 string str;
cout<<str<<endl;
这样语句不能执行
要使用string的一个函数进行转化 c_str() 才能正确使用其中的字符串。
完整的程序代码如下:
所创建的是一个Console 的工程
#include "stdafx.h"
#include "iostream.h"
#include <string>
using namespace std;
struct Rec
{
char chNum;
string path;
};
int main(int argc, char* argv[])
{
printf("Hello World!/n");
string out[10]=
{
"0000000000","1111111111","22222222222","333333333333","4444444444"
,"555555","6666666666","7777777","888888","999999"
};
struct Rec recInfo[10];
int cnt = 0 ;
for (int j=0; j<10; j++)
{
cnt +=out[j].length();
}
char *chrNew = new char[cnt+10];
int k = 0;
for (int i=0;i<10;i++)
{
/* cout<<endl<<out[i].length()<<endl;
cnt +=out[i].length();
recInfo[i].chNum = (char)(sizeof(out[i]));
recInfo[i].path = out[i];
*/
/* cnt = out[i].length();
recInfo[i].chNum = (char)*&cnt;
recInfo[i].path = out[i];
cout<<endl<<(int)recInfo[i].chNum<<" path = "<<recInfo[i].path.c_str()<<" length ="<<recInfo[i].path.length()<<endl;
*/
cnt = out[i].length();
// chrNew[k++] = (char)cnt;
char *chTmp = new char[cnt];
strcpy(chTmp,out[i].c_str()); // 严重错误,没有判断是否分配成功就直接拷贝!!!
/*
如何把string常量赋值给char数组呢???
切记切记 string 变量不能直接使用,如果想使用其中的字符串,需要用函数c_str()!!!
*/
for (int m=0; m<cnt; m++)
{
chrNew[k++] = chTmp[m];
}
}
// cout<<"sizeof(Rec) = "<<sizeof(Rec);
cout<<endl<<"cnt = "<<cnt<<endl;
for (int ik=0; ik<cnt+10; ik++)
{
cout<<chrNew[ik]+65;
}
return 0;
}
使用下面语句
#include <string>
没有.h,
否则无法引用标准模板库
2.使用命名空间
即使用下面语句:
using namespace std;
3.string 所创建的对象不能够直接使用里面的字符串
如 string str;
cout<<str<<endl;
这样语句不能执行
要使用string的一个函数进行转化 c_str() 才能正确使用其中的字符串。
完整的程序代码如下:
所创建的是一个Console 的工程
#include "stdafx.h"
#include "iostream.h"
#include <string>
using namespace std;
struct Rec
{
char chNum;
string path;
};
int main(int argc, char* argv[])
{
printf("Hello World!/n");
string out[10]=
{
"0000000000","1111111111","22222222222","333333333333","4444444444"
,"555555","6666666666","7777777","888888","999999"
};
struct Rec recInfo[10];
int cnt = 0 ;
for (int j=0; j<10; j++)
{
cnt +=out[j].length();
}
char *chrNew = new char[cnt+10];
int k = 0;
for (int i=0;i<10;i++)
{
/* cout<<endl<<out[i].length()<<endl;
cnt +=out[i].length();
recInfo[i].chNum = (char)(sizeof(out[i]));
recInfo[i].path = out[i];
*/
/* cnt = out[i].length();
recInfo[i].chNum = (char)*&cnt;
recInfo[i].path = out[i];
cout<<endl<<(int)recInfo[i].chNum<<" path = "<<recInfo[i].path.c_str()<<" length ="<<recInfo[i].path.length()<<endl;
*/
cnt = out[i].length();
// chrNew[k++] = (char)cnt;
char *chTmp = new char[cnt];
strcpy(chTmp,out[i].c_str()); // 严重错误,没有判断是否分配成功就直接拷贝!!!
/*
如何把string常量赋值给char数组呢???
切记切记 string 变量不能直接使用,如果想使用其中的字符串,需要用函数c_str()!!!
*/
for (int m=0; m<cnt; m++)
{
chrNew[k++] = chTmp[m];
}
}
// cout<<"sizeof(Rec) = "<<sizeof(Rec);
cout<<endl<<"cnt = "<<cnt<<endl;
for (int ik=0; ik<cnt+10; ik++)
{
cout<<chrNew[ik]+65;
}
return 0;
}
相关文章推荐
- 关于dsound.h的问题error C2501: 'DWORD_PTR' : missing storage-class or type specifiers
- error C2501: 'GetDocument' : missing storage-class or type specifiers 问题的处理
- error C2501: '_Application' : missing storage-class or type specifiers
- error C2501: 'AUX_RGBImageRec' : missing storage-class or type specifiers
- unexpected 'class***或error C2501: missing storage-class or type specifiers 错误解决办法
- 'IplImage' : missing storage-class or type specifiers
- '_ConnectionPtr' : missing storage-class or type specifiers
- 'HDEVNOTIFY' : missing storage-class or type specifiers
- Vc error C2629: unexpected 'class***或error C2501: missing storage-class or type specifiers
- 简单的问题,用类创建一个对象error C2380: type(s) preceding 'Complex' (constructor with return type, or illegal redefinition of current class-n
- missing storage-class or type specifiers的原因及解决办法
- 有关error C2501: 'WINGDIAPI' : missing storage-class or type specifiers的错误
- jsf配置问题 Error performing conversion of value ' of type class java.lang.String to type class
- [VC++]missing storage-class or type specifiers
- 解决 C++错误 error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class CString' (or there is no
- The type or namespace name 'Windows' does not exist in the class or namespace 'System'
- 做MFC的时候引用了头文件但VC还是报missing storage-class or type specifiers
- '_ConnectionPtr' : missing storage-class or type specifiers
- 'IplImage' : missing storage-class or type specifiers
- warning C4518: '__int64 ' : storage-class or type specifier(s) unexpected here; ignored