简体   繁体   English

C ++为类调用构造函数

[英]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. 我是C ++较大概念的新手,正在尝试编写一个小程序来测试构造函数和解构函数的行为。 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. 但是,由于关于myClass :: myClass的未定义引用或无法直接调用构造函数的愚蠢错误,我似乎无法使程序编译。

My code is as such: 我的代码是这样的:

myClass.h: myClass.h:

#pragma once

class myClass{

int x;

public:
    myClass(int n_x);

    myClass();

    ~myClass();
};

myClass.cpp: 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: 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. 来自Java背景。

====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. 无论出于何种原因,都会在项目目录中创建myClass.h和myClass.cpp文件,但未在代码树中的代码目录:: 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. 在“ myClass.cpp”中,定义函数时需要完全限定函数的名称。 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. 编译器不知道您是否在没有完整限定的情况下定义myClass成员函数。 As far as it's concerned, you could be defining some free function named myClass . 就其而言,您可以定义一些名为myClass自由函数。

Also, you need to include <iostream> in order to use std::cout . 另外,您需要包括<iostream>才能使用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. 之所以起作用,是因为new运算符是向后传递指针的函数,该指针可以转换为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. 我不知道任何Java,但是在有印象的人检查过C ++代码之后,我可能会想到Java程序员可能会喜欢显式构造函数。 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. 在C ++中,构造函数被更隐式地处理,子类上的EG构造函数链不需要(甚至不支持)显式调用,它们会自动调用继承链。

Long story short, don't treat constructors and destructors like normal function like methods to implement, treat them like mandatory, implicitly managed resource handlers. 长话短说,不要将构造函数和析构函数视为像普通函数那样的实现方法,而应将它们视为强制性的,隐式托管的资源处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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