Pedro
Pedro

Reputation: 331

C++ Object Instantiation Error in NetBeans

I'm having a very strange problem with my code. I have the following class:

    class Test
    {
        int a;
        string name;
     public:
        Test(){a = 0; name = "foo";}
        void setArguments(int number, string b){
            name = b;
            a = number;
        }
    };

And when I try to call the constructor using

    int main(void){
        Test a = Test();
        return 0;
    }

I can build the code, but can't run it. I can't even debug it. Any ideas of what's wrong?

EDIT 1: I'm starting to think that is a Compiler related problem. I'm using MinGW along with MSYS and the C++11 standard

Upvotes: 0

Views: 225

Answers (3)

Pacha
Pacha

Reputation: 1526

Probably you have a C#/Java background, that is why you try to instantiate your object a using that syntax. You can solve your problem using the following:

int main(void){
    Test a;
    return 0;
}

Objects that are not pointers in C++ must always call the constructors when they are declared.

Upvotes: 0

cubuspl42
cubuspl42

Reputation: 8400

Does this code even build? There is no semicolon after class body. There is not closing } for setArguments.

Upvotes: 1

deimus
deimus

Reputation: 9893

The code you have in your main basically does following :

The temporary object gets copied using the copy constructor to a. A better way is to write the following to avoid temporaries: Test a;

Upvotes: 0

Related Questions