C++ can't implement default constructor -


i have following class:

class fraction {     private:         int x;         int y;     public:     // constructors         fraction(long x = 0, long y = 1);         fraction(const fraction&)=default;//here problem         virtual ~fraction();  }; 

i'm trying disable default c++ constructor implement own (i intend use copy). so, declared default. but, when i'm trying implement it:

fraction::fraction(const fraction&){} 

compiler throws following error @ me:

./src/fraction.cpp:16:1: error: definition of explicitly-defaulted ‘fraction::fraction(const fraction&)’ fraction::fraction(const fraction&){ ^ in file included ../src/fraction.cpp:8:0: ../src/fraction.h:22:2: error: ‘fraction::fraction(const fraction&)’ explicitly defaulted here fraction(const fraction&)=default;

is there way fix it? i'm doing wrong ? found articles defaults, nothing can me fix these errors.

= default tells compiler use default implementation. means you cannot provide own. if don't want compiler create one, remove = default:

class fraction { // ...     fraction(const fraction&); // no =default }; 

by way, "default constructor" 1 that's called when don't provide arguments, e.g.

fraction my_frac; // default constructor called 

if want disable default constructor, use =delete:

class fraction{ public:     fraction() = delete; } 

keep in mind current fraction(long, long) provides default arguments, it's eligible default constructor.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -