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

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

我在初始化使用两个头文件的对象时遇到问题。一个头文件将值堆叠到一个数组中,该数组由另一个头文件组成。我使用单独的主脚本对 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

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

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

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

请不要介意我的英文。

编辑:

谢谢你们。这确实是问题所在。

这是因为您没有为 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 添加默认构造函数或向 Stack 添加以下初始化来解决此问题:

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