Default constructor

Why,after I initialize an object the print function is giving me error
Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
class A
{
public:
    A()
        :varI(0),varD(0)
    {
    }
    void print()
    {
        cout<<"int:"<<varI<<" double:"<<varD<<endl;
    }
private:
    int varI;
    double varD;
};
int main()
{
    A o1();
    o1.print();
}
A o1();

This is declaring the existence of a function named o1 that takes no parameters and returns an object of type A.
Because your definition

A o1();

the compiler considers as a definition of a function that has return type A and has no parameters.

To correct your definition you should write

A o1;
Last edited on
Thanks
Spasiba:))))
Last edited on
Topic archived. No new replies allowed.