Compilation error: C2679

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Compilation error: C2679

      Was getting

      Source Code

      1. error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)


      on building solution wherever there was an instance of:

      Source Code

      1. foo = NULL;


      where foo is a shared_ptr<bar>

      A quick google threw up that you can set a shared pointer to null by doing this:

      Source Code

      1. foo = shared_ptr<bar>();


      So I swapped them all out (3 of them, I think) to this syntax.

      There was also:

      Source Code

      1. error C2440: '<function-style-cast>' : cannot convert from 'int' to 'std::tr1::shared_ptr<_Ty>'


      Wherever there was an instance of:

      Source Code

      1. shared_ptr<bar>(NULL)


      Following my Googling, I just deleted the NULL parameter.

      Whether or not I've done good or bad, I have no idea - but at least the game builds and runs. I've had a few of this niggly compiler errors pop up on a couple other 3rd party (to me) projects I've built recently that only one other person (I'm aware of) has been able to reproduce, so quite possibly something a bit funky on my end.

      So really just a heads up for anyone else experiencing this problem.
    • You have to think of shared_ptr as a template class whos job is to handle the lifecycle of a pointer.

      NULL is just a typedef which is defined as:

      Source Code

      1. typedef NULL 0

      It's not some sort of special type in the language.

      So when you say:

      Source Code

      1. foo = NULL

      it thinks your trying to convert an integer to your type foo.
      I think this might actually work if your class foo had overloaded the = operator to handle integers on the right side.

      Source Code

      1. foo = shared_ptr<foo>(NULL)

      does not work because shared_ptr is trying to find a constructor for foo which takes an integer as a parameter.

      so basically if you want to make a NULL shared_ptr

      Source Code

      1. foo = shared_ptr()

      is the correct thing to do.
    • The first problem you come across is caused because the '=' operator on a shared_ptr is overloaded to accept another shared_ptr, not a raw pointer.

      First make sure that NULL is defined, I don't use the Win32 Lib's so I have to define it myself.

      Whenever I want to make a blank shared_ptr I just construct it without a parameter like you did and the class is set up to automatically NULL the internal pointer.

      I don't see why putting NULL in the parameter list would give you that error, to show you what it is looking for though this is how the template works.

      The shared_ptr has alot of overloaded constructors, but in this case it would look something like this

      Source Code

      1. template<typename Ty>
      2. shared_ptr(Ty* pointer);


      So when you put a class in this happens

      Source Code

      1. shared_ptr<bar>(bar* pointer);


      So you should have no problem passing in a NULL pointer

      error C2440: '<function-style-cast>' : cannot convert from 'int' to 'std::tr1::shared_ptr<_Ty>'

      This error may be because there was no appropriate constructor found for an integer value (your NULL integer), so the compiler is interpreting it as a function style cast, do a quick google search on 'function style cast error' , there are some things that aren't safe to do in Visual Studio, and I believe you have to be careful with function style casts
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz

    • NULL is just a typedef which is defined as:

      Source Code

      1. typedef NULL 0



      I don't think that would work, what you are saying is to make a new type called '0' which is in reference to the NULL constant. You can't start variables or functions with a number in C++ , and the typedef it more for simplifying longer types like

      Source Code

      1. typedef unsigned long ULONG;
      2. typedef char BYTE
      3. ULONG i = 10;
      4. BYTE c = 't';


      NULL will either be

      Source Code

      1. #define NULL 0
      2. or it will be a constant
      3. const int NULL = 0;


      Correct me if I'm wrong
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz
    • lol
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz