简体   繁体   中英

c++ Calling a constructor for a class

I'm new to the bigger concepts of C++ and am attempting to write a small program to test the behaviors of constructors and deconstructors. However I cannot seem to get the program to compile because of silly errors about undefined references to myClass::myClass or not being able to call a constructor directly.

My code is as such:

myClass.h:

#pragma once

class myClass{

int x;

public:
    myClass(int n_x);

    myClass();

    ~myClass();
};

myClass.cpp:

#include <string>
#include "myClass.h"

myClass(int n_x)
:x(n_x)
{
// constructor
std::cout << "A myClass object was created! And x == " << x << "!" << std::endl;
}

myClass()
:x(10)
{
// default constructor
std::cout << "A myClass object was created! And x == " << x << "!" << std::endl;
}

~myClass()
{
// deconstructor
std::cout << "A myClass object was destroyed!" << std::endl;
}

main.cpp:

#include <iostream>
#include "myClass.h"

using namespace std;

void func();
myClass* func2();

int main()
{
cout << "Hello world!" << endl;

myClass obj1;

func();

myClass* obj2 = func2();

delete obj2;

cout << "Goodbye world!" << endl;

return 0;
}

void func()
{
myClass obj1 = myClass(15);
}

myClass* func2()
{
myClass* obj1 = new myClass(5); // using the new operator to allocate memory in free space (the heap)

return obj1;
}

Am I obviously doing something wrong? Coming from a Java background here.

====EDIT==== Found out what was wrong. Now I feel dumb. For whatever reason the myClass.h and myClass.cpp files were created and in the directory of my project but not shown in the project tree in code::blocks. Once I added the files to the project it compiled successfully.

Thank you for your time and answers. Even though my issue was not really related to my code I still learned some things from your replies.

In "myClass.cpp", you need to fully qualify the names of the functions when you define them. Ie

myClass::myClass(int n_x): x(n_x) {
//...
}

The compiler doesn't know that you're defining member functions of myClass without the full qualification. As far as it's concerned, you could be defining some free function named myClass .

Also, you need to include <iostream> in order to use std::cout .

Beside the answer you already got about implementing things in their right name space, and since you seem to want to know more about constructor/destructor in first place:

When you see

myClass* whatever = new myClass();

work it's because the new operator is the one handing a pointer back that can be cast to a pointer of type myClass.

I don't know any Java, but having reviewed C++ code by people who do I'm under the impression there might be something like explicit constructors Java programmers might be used to. In C++ constructors are more implicitly handled, EG chains of constructors on subclassing don't need (don't even support) explicit invocation, they are automatically called up the chain of inheritance.

Long story short, don't treat constructors and destructors like normal function like methods to implement, treat them like mandatory, implicitly managed resource handlers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM