Search code examples
c++constructortype-conversionintellisensefriend-function

Visual Studio 2017: How to make intellisense accept friend conversion constructor from other class?


I am following a book with chapter about conversions from 2008. Compiling on visual studio 2017 C++ project.

In the book there was an example on the use of conversion constructor where classes "complex" and "number" exist and "number" can be converted into "complex" through the use of constructor where class "number" befriends the constructor of class "complex" to let it use "number" private properties (as much as I understand).

And the code sample from the book (copied word to word) makes intellisense not happy as it doesn't see friend "complex(number)" constructor and I do not know why.

Code is as below:

#include <string>

class number;
class complex;

int main()
{
    return 0;
}

class complex
{
private:
    double real;
    double imaginary;
public:
    complex(double r = 0, double i = 0) : real(r), imaginary(i) {}
    complex(number);
};

class number
{
    double n;
    std::string description;

    //friend complex::complex(number); // finds no instance of overload of function complex::complex

public:
    number(int k, std::string t = "no description") : n(k), description(t) {}
};

complex::complex(number ob)
{
    //real = ob.n;  //no friend, no access to private property
    imaginary = 0;
}

My question is, why is "friend complex::complex(number);" not visible by intellisense ?

Image of error from the IDE image of IDE error


Solution

  • You can think of this as a bug in itself. But you can work around it by hiding the code in question. For example,

    #ifdef __INTELLISENSE__
        #define INTELLIHIDE(...) // Hide from IntelliSense
    #else
        #define INTELLIHIDE(...) __VA_ARGS__
    #endif
    

    So, then you could do this:

        INTELLIHIDE(friend complex::complex(number);)
    

    And also,

    complex::complex(number ob)
    {
        INTELLIHIDE(real = ob.n;)
        imaginary = 0;
    }