How do I get rid of this" conversion from 'size_t' to 'long'" error?

vVector.size() gets the size of my vector.

I want to divide the size of my vector into a long variable, longTotal.

longTotal / vVector.size() yields this error:

warning C4267: '=' : conversion from 'size_t' to 'long', possible loss of data

I realize .size() returns a size_t, and it's not a long.  However, I do need to divide the long variable longTotal by the size of the vector...What do I do to get rid of this error?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

trinitrotolueneDirector - Software EngineeringCommented:
this is just a warning and is not an error. But this doesnt mean you should ignore it since warnings could provide indications of future errors.

do the following

long vecsize = static_cast<long>(vVector.size());


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trinitrotolueneDirector - Software EngineeringCommented:
just to add size_t is always guaranteed to be an integral unsigned type but the number of bits it takes depends on the environment that you are compiling on.
You can do longTotal / (long)vVector.size().

size_t type:

size_t is defined in a typedef. In your case it is, probably, unsigned long. So the compiler says that you are dividing a long integer value by an unsigned long.
evilrixSenior Software Engineer (Avast)Commented:
The real solution is stop using the incorrect type when handling the size of STL containers. They all use size_t (well, they actually all use ::size_type but this is generally defined as a size_t) and if you don't use this type yourself you are running the risk of writing non-portable code.

Using a static cast to remove the warning is not a solution it is a work around and is a poor way to handle this! as stated above, "you should ignore it since warnings could provide indications of future errors" and yet a cast is nothing more than a way to tell the compiler to ignore the warning.
size_type      Unsigned integral type (usually same as size_t)
size_t corresponds to the integral data type returned by the language operator sizeof and is defined in the  header file (among others) as an unsigned integral type.

NB. there is nothing in the standard to say it'll be an unsigned long -- it's just guaranteed to be an unsigned integer type.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.