S
subramanian100in
Consider the program
#include <iostream>
#include <string>
class A {
public:
void print(void) const;
A(const std::string &s) : str(s) { }
private:
std::string str;
};
void A:rint(void) const
{
std::cout << "A:rint() - " << str << '\n';
return;
}
int main(void)
{
A obj;
obj.print();
return 0;
}
If I compile this program under Linux, I am getting the compilation
error
error: no matching function for call to `A::A()'
for the line
A obj;
I thought the compiler will provide the default constructor A::A().
But it doesn't seem to. Kindly explain the reason.
If I remove my constructor
A(const std::string &s) : str(s) { }
then it compiles well and the compiler provides the default
constructor. Why is this difference ?
#include <iostream>
#include <string>
class A {
public:
void print(void) const;
A(const std::string &s) : str(s) { }
private:
std::string str;
};
void A:rint(void) const
{
std::cout << "A:rint() - " << str << '\n';
return;
}
int main(void)
{
A obj;
obj.print();
return 0;
}
If I compile this program under Linux, I am getting the compilation
error
error: no matching function for call to `A::A()'
for the line
A obj;
I thought the compiler will provide the default constructor A::A().
But it doesn't seem to. Kindly explain the reason.
If I remove my constructor
A(const std::string &s) : str(s) { }
then it compiles well and the compiler provides the default
constructor. Why is this difference ?