lundi 29 mars 2021

error: request for member ‘..’ in ‘..’, which is of non-class type ‘..’

I started a simple big integer library project, and decided to take it up again. But I can get over this error I am getting.

template <typename T>
bigint(T a)
{
        // Constructor for bigint passed
        // Let's call this 'A'
        if (std::is_same<T, bigint>::value)
        {
            this -> n = a.n;
        }

        // Check if passed argument is a float
        else if (std::is_same<T, float>::value or std::is_same<T, double>::value)
        {
            // Type-caste to lose fractional part
            this -> n = std::to_string((long long) a);
        }

        // Check if passed argument is a string
        else if (std::is_same<T, const char*>::value or std::is_same<T, std::string>::value)
        {
            this -> n = a;
        }



        // Straight cast for integers
        // Let's call this 'B'
        else
        {
            this -> n = std::to_string(a);
        }
}

Now when I call the constructor for an int or any other data-type, the first bigint constructor A is called, when B should be called, even though the types are different.

>>> bigint a(100);
bigint.h:42:18: error: request for member ‘n’ in ‘a’, which is of non-class type ‘int’
   42 |    this -> n = a.n;
      |                ~~^

Aucun commentaire:

Enregistrer un commentaire