警告C4267并警告C4996 [英] warning C4267 and warning C4996

查看:91
本文介绍了警告C4267并警告C4996的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中收到两条警告但无法弄清楚原因。将

有人看看这个并告诉我我做错了什么?


#include< iostream>


使用std :: cout;


#include< string>


使用std :: string;


class PPG {


public:


PPG(char * a,char b,int c)


{dresscolor = b;


power = c;


setname(a);} //结束构造函数1

PPG()


{setname(&#Bellum女士);


dresscolor =''p'';


power = 0;


} //结束默认构造函数


char * getname()const {返回名称;}

void setname(char * a){


int l = strlen (a);


name = new char [l + 1];


strcpy(name,a);


name [l] =''\ 0'';


} //结束setname


int getpower()const {return power;}


void setpower(int z){power = z;}

char getdresscolor()const {return dresscolor;}


void setdresscolor(char v ){dresscolor = v;}


void print()const


{cout<< name<< "喜欢穿;


开关(dresscolor){


案例''g'':案例''G'':


cout<<"绿色连衣裙。她用她的;休息;


案例''b'':案例''B'':


cout<<<" blue dresses。她使用她的;休息;


案例''''''案例''P'':


cout<< ;粉红色连衣裙。她用她的;


} //结束开关


if(power == 1)


cout<< 冰呼吸打败她的敌人。\ n;


否则if(power == 2)


cout<< ; 能够与松鼠交谈以混淆邪恶的villians.\ n;


否则if(power == 3)


cout <<<"恶劣的态度阻止邪恶的行动者。\ n" ;;


其他


cout<<"女孩权力统治世界。\ n" ;;


} //结束打印


bool operator ==(PPG& ppg )


{return(strcmp(name,ppg.name)== 0); }


私人:


char * name;


char dresscolor; // g-reen,b-lue,p-pink

int power; // 1-ice breath,2-squirrel speak,3-bad attitude


}; //结束课


#include" ppg.h"


#include< iostream>


int main()


{


PPG girl1(" Bubbles",''b'',2);


girl1.print();


PPG badgirl(公主,g,4);


badgirl.print();

返回0;


} //结束主要

这些是我得到的警告。


警告C4267:''初始化'':从''size_t''转换为''int'',可能

数据丢失


警告C4996:''strcpy''被宣布弃用


我也得到这个。


程序文件\ microsoft visual studio 8 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ''

解决方案

B.威廉姆斯写道:


我在代码中收到两条警告但无法弄清楚原因。将

有人看这个并告诉我我做错了什么?



没什么,真的。这些只是警告。


[...]


这些是我得到的警告。


警告C4267:''初始化'':从''size_t''转换为''int'',

可能丢失数据

>
警告C4996:''strcpy''被宣布弃用


我也得到了这个。


程序文件\ microsoft visual studio 8\vc\include\string.h(73):请参阅

声明''strcpy''



停止使用C字符串并开始使用''std :: string''。


V

-

请删除资本''A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问


< BLOCKQUOTE> B中。威廉姆斯写道:


我在代码中收到两条警告但无法弄清楚原因。将

有人看看这个并告诉我我做错了什么?


#include< iostream>


使用std :: cout;


#include< string>


使用std :: string;


class PPG {


public:


PPG(char * a,char b,int c)


{dresscolor = b;


power = c;


setname(a);} //结束构造函数1

PPG()


{setname(&#Bellum女士);


dresscolor =''p'';


power = 0;


} //结束默认构造函数


char * getname()const {返回名称;}


void setname(char * a){


int l = strlen(a);


name = new char [l + 1];


strcpy(name,a);


name [l] =''\ 0'';


} //结束setname


int getpower()const {return power;}


void setpower(int z){p ower = z;}


char getdresscolor()const {return dresscolor;}


void setdresscolor(char v){dresscolor = v; }


void print()const


{cout<< name<< "喜欢穿;


开关(dresscolor){


案例''g'':案例''G'':


cout<<"绿色连衣裙。她用她的;休息;


案例''b'':案例''B'':


cout<<<" blue dresses。她使用她的;休息;


案例''''''案例''P'':


cout<< ;粉红色连衣裙。她用她的;


} //结束开关


if(power == 1)


cout<< 冰呼吸打败她的敌人。\ n;


否则if(power == 2)


cout<< ; 能够与松鼠交谈以混淆邪恶的villians.\ n;


否则if(power == 3)


cout <<<"恶劣的态度阻止邪恶的行动者。\ n" ;;


其他


cout<<"女孩权力统治世界。\ n" ;;


} //结束打印


bool operator ==(PPG& ppg )


{return(strcmp(name,ppg.name)== 0); }


私人:


char * name;


char dresscolor; // g-reen,b-lue,p-pink

int power; // 1-ice breath,2-squirrel speak,3-bad attitude


}; //结束课


#include" ppg.h"


#include< iostream>


int main()


{


PPG girl1(" Bubbles",''b'',2);


girl1.print();


PPG badgirl(公主,g,4);


badgirl.print();


返回0;


} //结束主要

这些是我得到的警告。


警告C4267:''初始化'':从''size_t''转换为''int' ',可能

数据丢失


警告C4996:''strcpy''被宣布弃用


我我也得到了这个。


程序文件\ microsoft visual studio 8 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ''strcpy''



习惯上注意哪一行会产生警告或错误。

我会的也是uggest你开始缩进你的代码(你的编辑可以

甚至可能为你做这个 - 看看编辑|高级,或类似于Visual Studio中的
。这使得阅读起来更容易。


至于警告,我猜你在第一次看到第一个时候你会分配给b
strlen()的结果,返回size_t(参见
http:/ /www.cppreference.com/stdstring/strlen.html )到一个int。

前类型是无符号的,而后者是有符号的。通过混合有符号和无符号类型,您可能会失去

正确的长度。将

变量类型更改为size_t。


第二个警告表示strcpy()已被弃用

因为它是不安全的。如果必须,请使用strncpy。周围更好的是

将完全放弃字符数组和C风格的字符串函数来支持std :: string。这就是C ++方式(参见
http://www.parashift.com/c++-faq-lit...html#faq-34.1) ,它将支付许多棘手的细节对你来说。


如果你必须使用strcpy(例如,如果你的教授或老板因为某些无聊的原因而需要它),请使用#pragma来禁用那条消息。


干杯! --M


仅供参考,Visual Studio的帮助功能可用于解密这些

编译器错误消息。 />

只需执行帮助 - >搜索,然后在框中键入C4267,或者键入C4996或

。你会得到一个很好的页面,关于错误是什么,如何修复

等等。


Michael


I am receiving two warnings in my code and can''t figure out why. Will
someone look at this and tell me what I have done wrong?

#include <iostream>

using std::cout;

#include <string>

using std::string;

class PPG{

public:

PPG(char *a, char b, int c)

{ dresscolor =b;

power = c;

setname(a);}//end constructor 1

PPG()

{ setname("Ms. Bellum");

dresscolor =''p'';

power = 0;

}//end default constructor

char * getname()const {return name;}
void setname(char *a){

int l =strlen(a);

name = new char[l+1];

strcpy(name,a);

name[l] = ''\0'';

}//end setname

int getpower() const{return power;}

void setpower(int z){power = z;}

char getdresscolor() const{return dresscolor;}

void setdresscolor(char v){dresscolor=v;}

void print() const

{ cout <<name << " likes to wear ";

switch (dresscolor){

case ''g'': case ''G'':

cout <<"green dresses. She uses her "; break;

case ''b'':case ''B'':

cout <<"blue dresses. She uses her ";break;

case ''p'': case ''P'':

cout <<"pink dresses. She uses her ";

}//end switch

if (power == 1)

cout << "ice breath to defeat her enemies.\n";

else if (power ==2)

cout << "ability to talk to squirrels to confuse evil villians.\n";

else if (power ==3)

cout <<"bad attitude to stop evil doers.\n";

else

cout <<"girl power to rule the world.\n";

}//end print

bool operator==(PPG &ppg)

{ return (strcmp(name, ppg.name)==0); }

private:

char * name;

char dresscolor; //g-reen, b-lue, p-pink

int power; //1-ice breath, 2- squirrel speak, 3-bad attitude

}; //end class

#include "ppg.h"

#include <iostream>

int main()

{

PPG girl1("Bubbles", ''b'', 2);

girl1.print();

PPG badgirl("Princess",''g'', 4);

badgirl.print();
return 0;

}//end main

These are the warnings I am getting.

warning C4267: ''initializing'' : conversion from ''size_t'' to ''int'', possible
loss of data

warning C4996: ''strcpy'' was declared deprecated

I am also getting this.

program files\microsoft visual studio 8\vc\include\string.h(73) : see
declaration of ''strcpy''


解决方案

B. Williams wrote:

I am receiving two warnings in my code and can''t figure out why. Will
someone look at this and tell me what I have done wrong?

Nothing, really. These are just warnings.

[...]

These are the warnings I am getting.

warning C4267: ''initializing'' : conversion from ''size_t'' to ''int'',
possible loss of data

warning C4996: ''strcpy'' was declared deprecated

I am also getting this.

program files\microsoft visual studio 8\vc\include\string.h(73) : see
declaration of ''strcpy''

Stop using C strings and start using ''std::string''.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


B. Williams wrote:

I am receiving two warnings in my code and can''t figure out why. Will
someone look at this and tell me what I have done wrong?

#include <iostream>

using std::cout;

#include <string>

using std::string;

class PPG{

public:

PPG(char *a, char b, int c)

{ dresscolor =b;

power = c;

setname(a);}//end constructor 1

PPG()

{ setname("Ms. Bellum");

dresscolor =''p'';

power = 0;

}//end default constructor

char * getname()const {return name;}
void setname(char *a){

int l =strlen(a);

name = new char[l+1];

strcpy(name,a);

name[l] = ''\0'';

}//end setname

int getpower() const{return power;}

void setpower(int z){power = z;}

char getdresscolor() const{return dresscolor;}

void setdresscolor(char v){dresscolor=v;}

void print() const

{ cout <<name << " likes to wear ";

switch (dresscolor){

case ''g'': case ''G'':

cout <<"green dresses. She uses her "; break;

case ''b'':case ''B'':

cout <<"blue dresses. She uses her ";break;

case ''p'': case ''P'':

cout <<"pink dresses. She uses her ";

}//end switch

if (power == 1)

cout << "ice breath to defeat her enemies.\n";

else if (power ==2)

cout << "ability to talk to squirrels to confuse evil villians.\n";

else if (power ==3)

cout <<"bad attitude to stop evil doers.\n";

else

cout <<"girl power to rule the world.\n";

}//end print

bool operator==(PPG &ppg)

{ return (strcmp(name, ppg.name)==0); }

private:

char * name;

char dresscolor; //g-reen, b-lue, p-pink

int power; //1-ice breath, 2- squirrel speak, 3-bad attitude

}; //end class

#include "ppg.h"

#include <iostream>

int main()

{

PPG girl1("Bubbles", ''b'', 2);

girl1.print();

PPG badgirl("Princess",''g'', 4);

badgirl.print();
return 0;

}//end main

These are the warnings I am getting.

warning C4267: ''initializing'' : conversion from ''size_t'' to ''int'', possible
loss of data

warning C4996: ''strcpy'' was declared deprecated

I am also getting this.

program files\microsoft visual studio 8\vc\include\string.h(73) : see
declaration of ''strcpy''

It is customary to note which line(s) generate the warnings or errors.
I''d also suggest you start indenting your code (your editor can
probably even do it for you -- look under Edit | Advanced, or something
like that in Visual Studio). It makes it MUCH easier to read.

As for the warnings, I''d guess that you''re seeing the first when you
assign the result of strlen(), which returns size_t (see
http://www.cppreference.com/stdstring/strlen.html) to an int. The
former type is unsigned, while the latter is signed. You risk losing
the correct length by mixing signed and unsigned types. Change the
variable type to size_t.

The second warning just indicates that strcpy() has been deprecated
because it is unsafe. Use strncpy if you must. Better all around would
be to drop character arrays and C-style string functions altogether in
favor of std::string. That''s the C++ way (see
http://www.parashift.com/c++-faq-lit...html#faq-34.1), and it
will handle many of the trickier details for you.

If you must use strcpy (e.g., if your professor or boss requires it for
some inane reason), use a #pragma to disable that message.

Cheers! --M


FYI, Visual Studio''s help function is useful in deciphering these
compiler error messages.

Just do Help->Search, and type C4267 into the box, or C4996, or
whatever. You''ll get a nice page about what the error is, how to fix
it, etc.

Michael


这篇关于警告C4267并警告C4996的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆