1
#include<iostream>
using namespace std;

class date{

    public:
    int d,m,y;
    static date default_date;

    public:
    date(int dd=0,int mm =0,int yy=0){
        d = dd? dd: default_date.d;
        m = mm? mm: default_date.m;
        y = yy? yy: default_date.y;
    }

    static void setdefault(int a,int b,int c);
};

void date::setdefault(int a,int b,int c){

    default_date = date(a,b,c);
}

int main(){

    date xmas;
}

while executing this code, vscode shows linker failure, while executing same code on online ide shows following error:

/tmp/ccEXPoPl.o: In function `date::setdefault(int, int, int)':
1f584c669a5ff2377c1509273858b0e8.cpp:(.text+0x3c): undefined reference to `date::default_date'
1f584c669a5ff2377c1509273858b0e8.cpp:(.text+0x45): undefined reference to `date::default_date'
/tmp/ccEXPoPl.o: In function `date::date(int, int, int)':
1f584c669a5ff2377c1509273858b0e8.cpp:(.text._ZN4dateC2Eiii[_ZN4dateC5Eiii]+0x19): undefined reference to `date::default_date'
1f584c669a5ff2377c1509273858b0e8.cpp:(.text._ZN4dateC2Eiii[_ZN4dateC5Eiii]+0x30): undefined reference to `date::default_date'
1f584c669a5ff2377c1509273858b0e8.cpp:(.text._ZN4dateC2Eiii[_ZN4dateC5Eiii]+0x48): undefined reference to `date::default_date'
collect2: error: ld returned 1 exit status

I cannot find what the error is?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402

0 Answers0