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
编译时,我得到一个错误,即无法调用匹配的函数当在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
{
}
相关文章:
- 两个文件使用彼此的功能-如何解决
- 结构包含在两个头文件中,这两个文件我都不拥有
- Levenshtein 两个文件的距离花费了太多时间
- 如何在c ++中以一行(水平)打印两个文件的数据?如给定的.这两种形式来自两个不同的文本文件
- 包括两个文件 C++ 之间的冲突
- 如何检测两个文件条目是否引用与Qt相同的物理文件系统?
- 比较两个文件夹的关系
- 连接两个文件时如何避免冗余尾随换行符?
- 在循环工作时,首先将两个文件读为向量,但仅次于迭代一次
- 比较两个文件中的两个中间人的最佳方法.C++
- 同时逐行阅读两个文件
- 包含两个C++文件的制作文件:如何选择要编译的文件
- 在 c++ 中对两个文件使用标头
- 同时阅读两个文件
- 如何连接两个文件夹/项目的生成文件,这两个文件夹/项目必须一起运行且独立运行
- 从其他两个文件中创建一个分类整数的文件
- C 程序处理两个文件,而不是一个文件
- 知道分割故障在哪里比较两个文件
- 如何计算两个文件中字符单词和行的总数,并将它们相加以得出总数
- 将相同数据写入两个文件的有效方法