Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:38:01+00:00 2026-05-30T08:38:01+00:00

I am getting compilation error is following code. I thought this should have worked

  • 0

I am getting compilation error is following code. I thought this should have worked in c++
Can anybody help me to understand what is wrong here.

template < typename elem_type>
elem_type *find2( std::vector<elem_type>& vec, elem_type value) {
    for ( int i = 0; i < vec.size(); ++i) {
        if ( vec[i] == value ) {
            return &vec[i];
        }
    }
    return 0;
}
int main( int argc, char **argv) {
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    std::vector<int> vec( arr, arr+10);     
    int value = 9;
    int *ptr1 = find2(vec,value);
}

following is the compilation error

1>          d:\personal\work\find\find\find.cpp(25) : see reference to function template instantiation 'elem_type *find2<int>(std::vector<_Ty> &,elem_type &)' being compiled
1>          with
1>          [
1>              elem_type=int,
1>              _Ty=int
1>          ]

compiler is Visual Studio 11

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:38:02+00:00Added an answer on May 30, 2026 at 8:38 am

    In this line,

        for ( int i = 0; i < vec.size(); ++i ) {
    

    you are comparing the signed int variable i to the unsigned size_t result of vec.size().

    The compiler warns because such a comparision is unsafe in the face of implicit promotions in C++. i is promoted to size_t. If i were (hypothetically) negative, that would produce a really huge value, and would thus produce an unexpected comparision result.

    A simple cure is to

        #include <stddef.h>
        typedef ptrdiff_t Size;
        typedef Size Index;
    

    and then do e.g.

        for ( int i = 0; i < Size( vec.size() ); ++i ) {
    

    You will probably get at least one answer recommending the apparently simpler

        for ( size_t i = 0; i < vec.size(); ++i ) {
    

    but this is problematic for the same reason that the compiler warned: using unsigned integers as numbers risks getting very weird and unexpected results, buggy results, due to implicit promotions and in general conversion from negative number to unsigned, or vice versa.

    Even better than the casting above, define a countOf function like

        template< class Container >
        Size countOf( Container const& c ) { return v.size(); }
    
        template< class Elem, Size n >
        Size countOf( Elem (&)[n] ) { return n; }
    

    and then write just

        for ( int i = 0; i < countOf( vec ); ++i ) {
    

    And best, forget about that indexing and use iterators:

        for ( auto it = vec.begin(); it != vec.end(); ++it ) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting this compilation error with the following code: error C2143: syntax error :
I'm getting this weird compilation error with eclipse in the following code block. I've
In the following code, I am getting a compilation error stating that I have
I am getting the following error while migrating VC6 code to VS2008. This code
I am getting the following error message. Any ideas how to resolve this? Compilation
Strangely I am getting compilation error in C++ for the following code. class A
In the following code i am getting compilation error:c2227:left of -> left must point
I am getting an compilation error not a statement for the below code. Not
Getting the above error in following code. How to rectify it. Thanks. Please look
Can anybody explain the working of following code...? interface myInterface{} public class Main {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.