hi!

I have created a class with 2 constructors and whenever i try to access it i got the error no match for to call int&

here is the code

class A{
      public:
      A(){};
      A(int i);
      
      int moj_broj;
      };

A::A(int i):moj_broj(i){}

int main()
{
    A something;   
    cout<<"before "<<something.moj_broj<<endl;
    something(5);    
    cout<<"after "<<something.moj_broj<<endl;
}

hi!

I have created a class with 2 constructors and whenever i try to access it i got the error no match for to call int&

here is the code

class A{
      public:
      A(){};
      A(int i);
      
      int moj_broj;
      };

A::A(int i):moj_broj(i){}

int main()
{
    A something;   
    cout<<"before "<<something.moj_broj<<endl;
    something(5);    
    cout<<"after "<<something.moj_broj<<endl;
}

Try doing this instead:
e.g.

class A{
      public:
      A(){};
      A(int i);
      
      int moj_broj;
      };

A::A(int i):moj_broj(i){}

int main()
{
    A something;   
    cout<<"before "<<something.moj_broj<<endl;
    something = A(5);    
    cout<<"after "<<something.moj_broj<<endl;
}

That should compile and run ok.
Note: in the first cout, you'll see some arbitrary value output because the value of moj_broj is uninitialised by the default constructor.
Cheers for now,
Jas

{edit: Dammit beaten to the punch..}

Edited 6 Years Ago by JasonHippy: beaten to the punch!

This article has been dead for over six months. Start a new discussion instead.