简体   繁体   English

如何在我的主程序c ++中使用我自己制作的Array头文件?

[英]How can I use my own made Array header file in my main program c++?

I have created my own header file, this is how we were asked to do it, but what arguments should I use in my main program to call this header file and create an Array. 我已经创建了自己的头文件,这就是我们被要求这样做的方法,但是我应该在主程序中使用哪些参数来调用这个头文件并创建一个数组。

My header file looks like this: 我的头文件如下所示:

#ifndef ARRAY_H
#define ARRAY_H

class Array {
public:

    Array(int size) : _size(0), _arr(0) {
        // Call resize to initialize oneself
        resize(size) ;
    }

    Array(const Array& other) : _size(other._size) {
        _arr = new double[other._size] ;

        // Copy elements
        for (int i=0 ; i<_size ; i++) {
            _arr[i] = other._arr[i] ;
        }
    }

    ~Array() {
        delete[] _arr ;
    }

    Array& operator=(const Array& other)
    {
        if (&other==this) return *this ;
        if (_size != other._size) {
            resize(other._size) ;
        }
        for (int i=0 ; i<_size ; i++) {
            _arr[i] = other._arr[i] ;
        }
    }

    double& operator[](int index) {
        return _arr[index] ;
    }
    const double& operator[](int index) const {
        return _arr[index] ;
    }

    int size() const { return _size ; }

    void resize(int newSize) {
        // Allocate new array
        double* newArr = new double[newSize] ;

        // Copy elements
        for (int i=0 ; i<_size ; i++) {
            newArr[i] = _arr[i] ;
        }

        // Delete old array and install new one
        if (_arr) {
            delete[] _arr ;
        }
        _size = newSize ;
        _arr = newArr ;
    }

private:
    int _size ;
    double* _arr ;
} ;

#endif
  1. Do not write implementation of methods in .h file. 不要在.h文件中编写方法的实现。 There are only a few exceptions when writing code in header file is justified and your case is not one. 在头文件中编写代码合理的并且您的案例不是一个时,只有少数例外。 You should move implementations to a cpp file inside your project. 您应该将实现移动到项目内的cpp文件中。 Read carefully: Why have header files and .cpp files in C++? 仔细阅读: 为什么在C ++中有头文件和.cpp文件?
  2. If you want to use your .h file, simply write #include "your-h-filename.h" . 如果要使用.h文件,只需编写#include "your-h-filename.h" You will then be able to use defined classes, variables and functions defined in your .h file. 然后,您就可以使用.h文件中定义的已定义的类,变量和函数。

You might want to read this: http://www.learncpp.com/cpp-tutorial/19-header-files/ 您可能需要阅读: http//www.learncpp.com/cpp-tutorial/19-header-files/

In any .cpp file where you want to use this class, assuming "Array.h" is the relative path to the header file from your .cpp file, put the line: 在任何要使用此类的.cpp文件中,假设“Array.h”是.cpp文件中头文件的相对路径,请输入以下行:

#include "Array.h"

Near the top of the file, before any function or type declarations. 在任何函数或类型声明之前,靠近文件顶部。 This ensures that the code in your array file is treated as if it were written at that point in that .cpp file. 这可以确保将数组文件中的代码视为在该.cpp文件中的那一点写入。

As a further note, you usually want to split the definition of your class methods into a separate .cpp file (presumably Array.cpp). 另外,您通常希望将类方法的定义拆分为单独的.cpp文件(可能是Array.cpp)。 For example, for resize the definition in your header file should be changed to simply: 例如,要resize ,头文件中的定义应该更改为:

void resize(int newSize);

and the full definition should be put in the .cpp file: 并且应将完整定义放在.cpp文件中:

void Array::resize(int newSize) {
// Allocate new array
double* newArr = new double[newSize] ;

// Copy elements
for (int i=0 ; i<_size ; i++) {
  newArr[i] = _arr[i] ;
}

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

相关问题 我如何获取和使用头文件<graphics.h>在我的 C++ 程序中? - How I can get and use the header file <graphics.h> in my C++ program? C ++如何将主类与头文件链接? - C++ how can I link my main class with my header file? 如何在我的 c++ 程序中使用和启动 exe 文件? - How can I use and launch an exe file inside my c++ program? 如何在C ++中将头文件中的初始化变量导出到main.cpp文件中? - How do I export an initialized variable from a header file to my main.cpp file in C++? 我如何在 c++ 中实现我自己的堆栈迭代器 - how can i implement my own stack iterator in c++ 如何使用C ++编译项目,以便在硬盘上创建项目的.dll文件,以便以后将其与其他程序一起使用? - How can I compile my project in C++ so it will create on my hard disk a .dll file of my project to use it later with another program? 我的 C++ 程序如何才能等到对命名的 pipe 进行新的写入? - How can my C++ program wait until a new write has been made to the named pipe? 如何在 C++ 程序中使用下标数字? - How do I use subscript digits in my C++ program? c++ 程序如何返回我的数组或写入全局变量 - c++ program how can I return my array or write to a global variable 如何在我的c ++程序中将GDAL库用作可选库? - How can I use GDAL library as optional in my c++ program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM