Jump to content
  • Advertisement
Sign in to follow this  
asdqwe

impossible w/o casting

This topic is 3821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've heard casts have to be avoided as much as possible, and can always be avoided. But SOME functions seem to require them. EG:
void main()
{
		int nr;
		nr = strlen("Babe Ruth");
		cout<< nr;
}
That way, I get: warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data. But when I change the offender into:
size_t nr;
I get: warning C4267: 'argument' : conversion from 'size_t' to 'unsigned int', possible loss of data. So I'm asking: Can anyone, including Bill Gates, fix this so it doesn't spit warnings OR require typecasts?

Share this post


Link to post
Share on other sites
Advertisement
Use C++ (the C++ STL in particular):

void main()
{
size_t nr;

nr = std::string("Babe Ruth").size();
std::cout << nr;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by asdqwe
But when I change the offender into:
size_t nr;
I get: warning C4267: 'argument' : conversion from 'size_t' to 'unsigned int', possible loss of data.
So I'm asking: Can anyone, including Bill Gates, fix this so it doesn't spit warnings OR require typecasts?

My compiler doesn’t give any error or warning using size_t in your code.

Share this post


Link to post
Share on other sites
To nife87: your code still is buggy. Now I get a warning on the std::cout line: warning C4267: 'argument' : conversion from 'size_t' to 'unsigned int', possible loss of data
So it's no good!

Share this post


Link to post
Share on other sites
It sounds like you have the /Wp64 flag enabled for your project. Using this flag gives a lot of spurious warnings. Go to your project settings and disable "Detect 64-bit Portability Issues" in the C++/General section.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!