lundi 2 octobre 2017

Why delegating constructor is not working with gcc 5.3.1?

Why isn't this test program that I wrote isn't working, in spite of enabling ' -std=c++11' switch to g++ ?

class myClass {
    public:
        myClass(string ss="defaultvalue2", int ii=0, double ff=0.0):
             s(ss), i(ii), f(ff) {}

        myclass(): myClass("value1", 1, 2.1) {}

        void show() {
            cout << "s = " << s << endl;
        }

    private:
        string s="defaultvalue1";
        int i = 0;
        double f = 0.0;


};

int main(int argc, char *argv[]) {

    myClass mc1;
    mc1.show();
    return 0;
}

$ make
g++ -std=c++11 -g -Wall -Wno-unused-but-set-variable -o test test.cpp
test.cpp:11:11: error: ISO C++ forbids declaration of ‘myclass’ with no type [-fpermissive]
   myclass(): myClass("value1", 1, 2.1) {}
           ^
test.cpp: In member function ‘int myClass::myclass()’:
test.cpp:11:14: error: only constructors take member initializers
   myclass(): myClass("value1", 1, 2.1) {}
              ^
test.cpp:11:41: warning: no return statement in function returning non-void [-Wreturn-type]
   myclass(): myClass("value1", 1, 2.1) {}
                                         ^
Makefile:2: recipe for target 'test' failed
make: *** [test] Error 1

GCC version is gcc (GCC) 5.3.1 20160406 (Red Hat 5.3.1-6)

Aucun commentaire:

Enregistrer un commentaire