error C2440 using adjacent_difference stl template function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: error C2440 using adjacent_difference stl template function

  1. #1
    Join Date
    Apr 2010
    Posts
    2

    error C2440 using adjacent_difference stl template function

    Hi all,
    I would like to use STL <numeric> adjacent_difference function in my program but I'm getting the error:

    1>c:\program files\microsoft visual studio 8\vc\include\numeric(349) : error C2440: '=' : cannot convert from 'Csimple' to 'int'
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.

    I have a <list> of Csimple's (udt) objects where I would calculate the differences (Csimple is a very simple class with only one int parameter called "firstInt").

    list<Csimple> simpleList;
    simpleList.push_back(Csimple(10)); //fill the list with Csimple's objects
    ...
    simpleList.push_back(Csimple(25));
    vector<int> res(simpleList.size());
    vector<int>::iterator v_resItr;
    v_resItr = adjacent_difference(simpleList.begin(), simpleList.end(), res.begin());
    I've also added the overloaded version of operator- and operator= as class members as suggest by errors provided by compiler

    Csimple Csimple:perator -(Csimple op2)
    { return firstInt - op2.firstInt; }

    Csimple Csimple:perator =(Csimple op2)
    { return firstInt = op2.firstInt; } //I'm not sure about the returned type, should be it an int?

    I assume that I can use adjacent_difference also with user-defined type rather than just int; may be my assumption wrong?

    Can someone help me, pls?

    Many thanks in advance

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: error C2440 using adjacent_difference stl template function

    Code:
    Csimple Csimple :: operator -(Csimple op2)
    { return firstInt - op2.firstInt; }
    
    Csimple Csimple :: operator =(Csimple op2)
    { return firstInt = op2.firstInt; }
    Both return a 'int', not a 'Csimple'

  3. #3
    Join Date
    Apr 2010
    Posts
    2

    Re: error C2440 using adjacent_difference stl template function

    Hi Skizmo,
    many thanks for your suggestion, now is a little bit better because I got just 1 errors C2440 (before was 2).

    int Csimple :: operator -(Csimple op2)
    { return firstInt - op2.firstInt; }

    int Csimple :: operator =(Csimple op2)
    { return firstInt = op2.firstInt; }

    I need to change just the return type or the parameter type as well?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center