还是关于string的

lingling1989r 2009-01-29 11:28:43
大家帮忙看下这几个问题:
1,
大概就是
char cp;
string str;
cp='a';
str=str+cp;//这么写没有问题吧??
cout<<str<<endl;

就是这样的一段代码,在我的vs2008里,编译可以,生成的时候就报错 error PRJ0003 : 生成“rc.exe”时出错。
之前自己问过的问题的帖子http://topic.csdn.net/u/20090124/17/11fe4751-b8a2-4e96-b210-3b67fea6da16.html?seed=1513915399

2,类似的代码

string str;
str=str+"中文";//这里有问题没?


这样的代码,问题同1.

3.代码如下

string str="up";
char *cp;
cp=new char[str.size()+1];
strcpy_s(cp,str.c_str());//就将之前的一个帖子里的代码改了strcpy改成了strcpy_s()就报错了
cout<<cp<<endl;


报错 error C2660: “strcpy_s”: 函数不接受 2 个参数,,这个函数不是可以的吗?之前用的时候,都直接把strcpy的地方直接替换都不会有问题的,这里为什么就报错了呢???

或者若是有装2008或2005的,帮看下在大家的机子上是不是也是如此,因为我的安装的时候就说我的2008安装有问题,所以,,麻烦大家了。。
...全文
234 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hityct1 2009-01-30
  • 打赏
  • 举报
回复
前两个2008测试没有错误。


#include "stdafx.h"
#include<string>
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char cp;
string str;
cp='a';
str=str+cp;
str=str+"中文";
cout<<str<<endl;

return 0;
}


ljmscsq 2009-01-30
  • 打赏
  • 举报
回复
1.

#include <iostream>
#include <string>
using namespace std;

int main()
{
char cp;
string str;
cp='a';
str=str+cp;
cout<<str<<endl;
return 0;
}


2.

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str;
str=str+"中文";
cout<<str<<endl;
return 0;
}


1和2的程序我没有08,在05下给你测了下是可以用的。我觉得是不是你08配置的问题。这个问题网上也有人问。有05的解决方法,没找到08的。

3.
strcpy_s(cp,str.c_str());
如果你用两个参数cp就别用指针。strcpy_s应该是通过sizeof确定大小的。如果cp是一个指针,sizeof(cp)=4,所以不安全。如果你用比如cp[20]
这样应该就行了,或者你直接用3个参数的就可以了
fibbery 2009-01-30
  • 打赏
  • 举报
回复
std::string在使用char * p;运算时,当p为NULL,可能会出现非法,视编译器不同而不同。好像G++中没有问题,VC中有问题。
waizqfor 2009-01-30
  • 打赏
  • 举报
回复
waizqfor 2009-01-30
  • 打赏
  • 举报
回复

string str="up";
char *cp;
cp=new char[str.size()+1];
strcpy_s(cp,str.c_str());//就将之前的一个帖子里的代码改了strcpy改成了strcpy_s()就报错了
cout<<cp<<endl;

LZ strcpy和strcpy_s本身就是有区别的

//函数声明
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);

strcpy_s的例子


// crt_strcpy_s.cpp
// This program uses strcpy_s and strcat_s
// to build a phrase.
//

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main( void )
{
char string[80];
// using template versions of strcpy_s and strcat_s:
strcpy_s( string, "Hello world from " );
strcat_s( string, "strcpy_s " );
strcat_s( string, "and " );
// of course we can supply the size explicitly if we want to:
strcat_s( string, _countof(string), "strcat_s!" );

printf( "String = %s\n", string );
}

xiaoyisnail 2009-01-30
  • 打赏
  • 举报
回复
前两个估计你的2008确实是有问题,最后一个要3个参数
arong1234 2009-01-30
  • 打赏
  • 举报
回复
再次强调要会用MSDN,你得rc.exe错误也是在MSDN有描述得


Error Message
Error spawning 'command line'.


A command, command line, which was formed from input in the Property Pages dialog box, returned an error code but no information will appear in the Output window.

Possible reasons for this error:

Your project depends on ATL Server. Beginning with Visual Studio 2008, ATL Server is no longer included as part of Visual Studio, but has been released as a shared-source project at CodePlex. To download the ATL Server source code and tools, go to http://go.microsoft.com/fwlink/ ? LinkID=81979.

Low system resources. Close some applications to resolve this.

Insufficient security privileges. Verify that you have sufficient security privileges.

The executable paths specified in VC++ Directories do not include the path for the tool that you are attempting to run.

For makefile projects, you are missing a command to run on either Build Command Line or Rebuild Command Line.


chin_chen 2009-01-30
  • 打赏
  • 举报
回复
1
#include <string>
#include <iostream>
using namespace std;
int main()
{
char cp;
string str;
cp='a';
str=str+cp;//这么写没有问题吧??
cout<<str<<endl;




system("pause");

}

没有问题,在vs2005下。
2.
#include <string>
#include <iostream>
using namespace std;

int main()
{
string str;
str=str+"中文";//这里有问题没?




system("pause");

}

也没有问题,在vs2005下
3。
 
#include <string>
#include <iostream>
using namespace std;

int main()
{
string str="up";
char *cp; cp=new char[str.size()+1];
strcpy_s(cp,sizeof(cp),str.c_str());//非strcpy_s(cp,str.c_str()),注意参数少一个
cout<<cp<<endl;


system("pause");

}

在vs2005下
hityct1 2009-01-30
  • 打赏
  • 举报
回复
strcpy_s有两种形式:

第一种:
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);

第二种,是个模板:
template <size_t size>
errno_t strcpy_s(
char (&strDestination)[size],
const char *strSource
); // C++ only



#include "stdafx.h"
#include<string>
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
string str="up";
char *cp;
cp=new char[str.size()+1];
//cout<<str.size()<<" "<<str.c_str()<<endl;
strcpy_s(cp,str.size()+1,str.c_str());//第一种形式
cout<<cp<<endl;

char cp2[3];
strcpy_s(cp2,str.c_str());//第二种形式
cout<<cp2<<endl;


return 0;
}

yangkunhenry 2009-01-29
  • 打赏
  • 举报
回复
strcpy_s也有带两个参数的。这样就可以

string str="up";
//char *cp=new char[str.size()+1];
char cp[3];
strcpy_s(cp,str.c_str());
cout<<cp<<endl;

MSDN上

template <size_t size>
errno_t wcscpy_s(
wchar_t (&strDestination)[size],
const wchar_t *strSource
); // C++ only
hangyu628 2009-01-29
  • 打赏
  • 举报
回复
第3个改这样 strcpy_s(cp,sizeof(str.size()+1),str.c_str());
我这边的VS2008通过,输出结果up
arong1234 2009-01-29
  • 打赏
  • 举报
回复
_s不是只是不加这个后缀函数的wrap,它的函数原型也变了,按照strcpy的用法肯定错
你稍微看看MSDN就知道它的函数原型是


errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);


这种简单的错误应该多看MSDN,而不是问别人

至于第一第二个问题,应该是你自己的VS2008没有安装好,rc.exe和你代码没有啥关系
hangyu628 2009-01-29
  • 打赏
  • 举报
回复
第3个的话,是你看错了,不是不接受第2个参数,而是
strcpy_s需要3个参数
hangyu628 2009-01-29
  • 打赏
  • 举报
回复
不好意思楼主,没看到你以前的帖子.有头文件...

不过,前两个在我的机子上正常通过...没什么问题
hangyu628 2009-01-29
  • 打赏
  • 举报
回复
前两个估计 缺少 头文件 <string>
我编译正常..

如下

#include <iostream>
#include <string>
using namespace std;

int main()
{
char cp;
string str;
cp='a';
str=str+cp;//这么写没有问题吧??
cout<<str<<endl;
getchar();
return 0;
}

64,683

社区成员

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

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