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

C++ C++;:如何初始化使用两个头文件的对象?,c++,initialization,header-files,C++,Initialization,Header Files,我在初始化使用两个头文件的对象时遇到问题。一个头文件将值堆叠到由另一个头文件生成的数组中。我使用单独的主脚本对堆栈定义文件进行计算。情况如下: 主脚本 #include <iostream> #include "Stack.h" #include "Array.h" using namespace std ; int main() { int LEN = 10; // size array double def_val = 1.1 ; //

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

主脚本

#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;
}
#包括
#包括“Stack.h”
#包括“Array.h”
使用名称空间std;
int main(){
int LEN=10;//大小数组
double def_val=1.1;//用于填充调整大小的数组的默认值

堆栈s(LEN,def_val);//这是因为您尚未为数组指定默认构造函数(
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
    }

似乎他应该/喜欢做的是
堆栈(int-size,double-value):s(size,value)
。。。
#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(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
    }
    Stack(int size, double value)
        : s(size, value)  // default initialize s
        , count(0)  // default initialize count
    {
    }