C++:如何初始化使用两个头文件的对象

C++ : How to initialize an object that uses two header files?

本文关键字:两个 文件 对象 初始化 C++      更新时间:2023-10-16

初始化一个使用两个头文件的对象时遇到问题。一个头文件将值堆叠到由另一个头文件组成的数组中。我使用一个单独的主脚本来对Stack定义文件进行计算。它看起来如下:

主脚本

#include <iostream>
#include "Stack.h"
#include "Array.h"
using namespace std ;
int main() {
int LEN = 10;               // size array
double def_val = 1.1 ;      // a default value that is used to fill a resized array
Stack s(LEN, def_val) ;     // <--- causing compiler error
// Do calculations with functions defined in a Stack.cc file
return 0;
}

堆栈头文件

#ifndef STACK_HH 
#define STACK_HH
#include <iostream>
#include "Array.h"
using namespace std ;
class Stack {
    public: 
        Stack(int size, double value) {
            s.size(size);
            s.value(value);    // <--- Not sure if I should use this, see Array header file how default value is used to resize an array
            count = 0
            //used member functions which are not important to solve this particular problem
        }
    // Member functions
    private:
       Array<double> s ;
       int count ;
};

数组头文件

#ifndef ARRAY_HH
#define ARRAY_HH
template <class T> 
class Array {
public:
  Array(int size, T value) : _size(size) {         // <--- takes two arguments
     _arr = new T[_size] ;
     _value = value ;       // Set default value
     std::cout << "default value = " << _value << std::endl ;
  }

  Array(const Array<T>& other) : _size(other._size), _value(other._value) {
    _arr = new T[other._size] ;
    _value = _value ;       
    // Copy elements
    for (int i=0 ; i<_size ; i++) {
        _arr[i] = other._arr[i] ;
    }
  }
 ~Array() {
    delete[] _arr ;
  }

  Array<T>& operator=(const Array<T>& 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] ;
    }
    return *this ;
  }
  T& operator[](int index) {
      if (index > _size) {          
          resize(index) ;
      }
      return _arr[index] ;
   }
  const T& operator[](int index) const {
      if (index > _size) {          
         resize(index) ;
      }
      return _arr[index] ;
  }
 int size() const { return _size ; }
 T value() const { return _value ; }   // <--- Included this for reading the default value from the object initialized in the main script, just like the size is read.
 void resize(int newSize) {
    // Allocate new array
    T* newArr = new T[newSize] ;
    // Copy elements
    for (int i=0 ; i<_size ; i++) {
        newArr[i] = _arr[i] ;
    }
    // Fill remaining array with default value
    for (int i=_size ; i<=newSize; i++){
        newArr[i] = _value ;
    }
    // Delete old array and install new one
    delete[] _arr ;
    _size = newSize ;
    _arr = newArr ;
 }

private:
  int _size ;
  T* _arr ;
  T _value ;
} ;
#endif

编译时,我得到一个错误,即无法调用匹配的函数当在Stack.h文件中读取Stack(int size,double value){}时,为"Array[double]::Array()"。

最初,只读取大小。然而,我意识到Array头文件需要第二个参数来创建数组。因此,我在Stack.h文件中包含了s.value()函数,类似于s.size()函数。然而,这并没有解决编译器错误。

这是我两周前制作的一个已经在工作的脚本的修改版本,但现在想扩展它的可用性,并将其变成一个模板类。

请不要介意我的英语。

编辑:

我感谢你们两个。这确实造成了问题。

这是因为您没有为Array指定默认构造函数(Array::Array()),当编译器看到以下内容时:

    Stack(int size, double value) {
        s.size(size);
        s.value(value);    // <--- Not sure if I should use this, see Array header file how default value is used to resize an array
        count = 0
        //used member functions which are not important to solve this particular problem
    }

你实际写的是:

    Stack(int size, double value)
        : s()  // default initialize s
        , count()  // default initialize count
    {
        s.size(size);
        s.value(value);    // <--- Not sure if I should use this, see Array header file how default value is used to resize an array
        count = 0
        //used member functions which are not important to solve this particular problem
    }

s()失败,因为这是Array的默认构造,并且没有Array的默认构造函数。

编辑:正如hr_117所指出的,您可以通过向Array添加默认ctor来解决此问题,也可以向Stack添加以下初始化:

    Stack(int size, double value)
        : s(size, value)  // default initialize s
        , count(0)  // default initialize count
    {
    }