0

I am running openSUSE Leap 15.3 with gcc 7.5.0. I have 4 files:

compile.sh

g++ -o polynom main.c++ polynom.c++

main.c++

#include "polynom.h"

int main (int argc, char **argv)
{
   double Y = Polynom::X(0.0);

   return(0);
}

polynom.h

#ifndef __POLYNOM_H__
#define __POLYNOM_H__

class Polynom
{
   public:

      Polynom(int R);
      ~Polynom();
      
      static double X(Polynom P);
};

#endif

polynom.c++

#include <stdlib.h>

#include "polynom.h"

Polynom::Polynom(int R)
{
};

Polynom::~Polynom()
{
};

double Polynom::X(Polynom P)
{
   return(-1.0);
};

I did

zypper install gcc-c++

g++ should complain about

   double Y = Polynom::X(0.0);

in file main.c++. If I remove "int R" from the Constructor, i get "no known conversion for argument 1 from 'double' ...".

user17732522
  • 53,019
  • 2
  • 56
  • 105
  • 2
    No, it should not complain. Why do you expect it to complain? `0.0` can be implicitly converted to `Polynom` via your converting constructor. – user17732522 Mar 14 '22 at 08:47
  • `#ifndef __POLYNOM_H__` -- Additional note -- The program is ill-formed due to you using double underscores as an identifier. Identifiers starting with double underscores are reserved for the compiler's usage. – PaulMcKenzie Mar 14 '22 at 08:53
  • 1
    You need to use `{}` or `explicit` to prevent narrowing. [Preventing narrowing conversion when using std::initializer_list](https://stackoverflow.com/q/16939471/995714), [What's the difference between parentheses and braces in c++ when constructing objects](https://stackoverflow.com/q/57304873/995714), [Avoid narrowing type conversion on function call](https://stackoverflow.com/q/26848248/995714) – phuclv Mar 14 '22 at 08:54
  • as said `__` at the beginning is prohibited: [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/q/228783/995714) – phuclv Mar 14 '22 at 08:55
  • I am terrible sorry about that. I did not understand. – Christian Reinbothe Mar 14 '22 at 09:42

0 Answers0