C4267 conversion from 'size_t' to 'int'

This is a discussion on C4267 conversion from 'size_t' to 'int' within the C++ Programming forums, part of the General Programming Boards category; Hello, I am getting two instances of the warning C4267 in my function. This warning says: 'initializing' : conversion from ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    49

    C4267 conversion from 'size_t' to 'int'

    Hello,

    I am getting two instances of the warning C4267 in my function. This warning says: 'initializing' : conversion from 'size_t' to 'int', possible loss of data

    I looked this up on the internet and it doesn't seem to be a great big deal, but I was wondering if there was a method I could use to rewrite the function so I can get a clean compile.

    Code:
    // This program will count the occurrences of a string within a string.
    #include <iostream>  // cin, cout, <<, >>
    #include <string>    // string
    using namespace std;
    
    
    int count;
    int occurCount(string quote)
    {
    	
     cout << quote << endl;   // quote will come from a driver file
    
     int position = quote.find ("a spider", 0);   // warning C4267
    	 if (position == string::npos)
    	 count = 0;
    	 else count = 1;
                            while (position != string::npos)
    	 {
    	 position = quote.find ("a spider", position + 1); //C4267
                 
    	 if (position > 0)
                     {
                     count++;				 
    	 }			 			 
    	 }
    	 return count;
    }
    I am sorry about the indentation/spacing. It looks alot better in Visual Studio.
    Semper Fi!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Simple, if it returns size_t, make position a size_t variable.
    Code:
    // This program will count the occurrences of a string within a string.
    #include <iostream>  // cin, cout, <<, >>
    #include <string>    // string
    using namespace std;
    
    
    int count;
    int occurCount(string quote)
    {
    	
     cout << quote << endl;   // quote will come from a driver file
    
     size_t position = quote.find ("a spider", 0);   // warning C4267
    	 if (position == string::npos)
    	 count = 0;
    	 else count = 1;
                            while (position != string::npos)
    	 {
    	 position = quote.find ("a spider", position + 1); //C4267
                 
    	 if (position > 0)
                     {
                     count++;				 
    	 }			 			 
    	 }
    	 return count;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    Gee, that was simple. Thanks!
    Semper Fi!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Simple, if it returns size_t, make position a size_t variable.
    While that particular implementation may use size_t, it's better to use the correct type that the function returns. In this case, string::size_type.
    Code:
    string::size_type position = quote.find ("a spider", 0);
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    >While that particular implementation may use size_t, it's better >to use the correct type that the function returns. In this case, >string::size_type.

    But would I still need to declare:

    Code:
    int position = quote.find ("a spider", 0);   // warning C4267
    as a type size_t, rather than int? It compiles cleanly as long as I declare it as type size_t. I think what I want to ask is what does adding string::size_type do for the code? I will use it regardless, because it looks cool, but I surely do not understand what it does.
    Semper Fi!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think what I want to ask is what does adding string::size_type do for the code?
    It makes it more portable.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    Thank you.
    Semper Fi!

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by Prelude
    >I think what I want to ask is what does adding string::size_type do for the code?
    It makes it more portable.
    Could you elaborate more on that portability issue? Are you saying that int will not compatible on some platform? or??
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Are you saying that int will not compatible on some platform?
    No, I'm saying that the return type of string::find() may not return int or size_t. You don't know, so you should use string::size_type to be sure that you're using the right type.
    My best code is written with the delete key.

  10. #10
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by Prelude
    >Are you saying that int will not compatible on some platform?
    No, I'm saying that the return type of string::find() may not return int or size_t. You don't know, so you should use string::size_type to be sure that you're using the right type.
    Okay, I'm back and get even more confused. You say, "string::find() may not return int or size_t???????"
    If I want to use string::find, do I not look it up and it tells me the function's arguments and what it returns as well as its return type ???
    Or are you saying that string::find from ,say ,.NET's library may differ from that of g++ in their return type??
    EDIT: wait a sec, size_type is not the same as size_t, huh. But size_type could be size_t and int??
    Last edited by alphaoide; 03-16-2004 at 04:42 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But size_type could be size_t and int??
    It could be either. It could be something different. It's implementation defined.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  2. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21