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;
}

Recommended Answers

All 3 Replies

Line #15 statement should be,

something=A(5);

Line #15 statement should be,

something=A(5);

ty it works now

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..}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.