我有一个从Array class继承的BaseArray class。在BaseArray中,我具有受保护的成员变量data_cur_size_Array类引入了resize函数。我遇到的问题是,在BaseArray函数中似乎没有访问resize中受保护的成员变量。

编辑:解决了max_size_问题,但cur_size_data_文件仍然存在

遗产?范围?救命?

错误:

In file included from Array.h:41:0,
                 from driver.cpp:6:
Array.cpp: In member function ‘void Array<T>::resize(size_t)’:
Array.cpp:29:5: error: ‘data_’ was not declared in this scope
     data_=data_;
     ^
Array.cpp:30:18: error: ‘cur_size_’ was not declared in this scope
     if (new_size>cur_size_)
                  ^
Array.cpp:37:5: error: ‘cur_size_’ was not declared in this scope
     cur_size_=new_size;
     ^


代码:

BaseArray.h:

#ifndef _BASEARRAY_H_
#define _BASEARRAY_H_

#include <cstring>

template <typename T>
class BaseArray
{
public:
  /// Type definition of the element type.
  typedef T type;
  //constructors, destructor and methods…
protected:
  /// Pointer to the actual data. m
  char * data_;

  /// Current size of the BaseArray.
  size_t cur_size_;

};

#include "BaseArray.inl"
#include "BaseArray.cpp"

#endif   // !defined _BASEARRAY_H_


Array.h:

#ifndef _ARRAY_H_
#define _ARRAY_H_

#include <cstring>
#include "BaseArray.h"
template <typename T>
class Array: public BaseArray<T> //inheriting from BaseArray
{
public:
  /// Type definition of the element type.
  typedef T type;

  /// Default constructor.
  Array (void);

  Array (const Array & arr);

  /// Destructor.
  ~Array (void);
  const Array & operator = (const Array & rhs);

  void resize (size_t new_size);

private:
  size_t max_size_; //introduces max_size
};

#include "Array.inl"
#include "Array.cpp"

#endif   // !defined _ARRAY_H_


Array.cpp:

#include "BaseArray.h"
#include "Array.h"
#include <stdexcept>
#include <iostream>
template <typename T>
Array <T>::Array (void): BaseArray<T>()

{
    std::cout<<"Array def const called"<<std::endl;
}

template <typename T>
Array <T>::Array (const Array & array): BaseArray<T>(array)
{
}

template <typename T>
Array <T>::~Array (void)
{
}


template <typename T>
void Array <T>::resize (size_t new_size)
{
this->data_= this->data_;
if (new_size>this->cur_size_)
    {
    max_size_ = new_size-this->cur_size_-1;
    this->cur_size_=new_size;
    for (max_size_; max_size_<=new_size; max_size_++)
        this->data_[max_size_]=0;
    }
this->cur_size_=new_size;

}

/* Also tried it like this:

template <typename T>
void Array <T>::resize (size_t new_size)
{
    BaseArray<T>::data_= BaseArray<T>::data_;
    if (new_size>BaseArray<T>::cur_size_)
        {
        max_size_ = new_size-BaseArray<T>::cur_size_-1;
        BaseArray<T>::cur_size_=new_size;
        for (max_size_; max_size_<=new_size; max_size_++)
            BaseArray<T>::data_[max_size_]=0;
        }
    BaseArray<T>::cur_size_=new_size;
} */

最佳答案

关于第一个错误,您在Array中没有声明max_size()成员。

关于第二个错误,模板中的名称查找遵循两步逻辑,即在定义点查找非从属表达式,而在实例化点查找从属表达式;

这意味着当编译器看到data_时,它会认为这是一个位于其他位置的变量。充其量,它不会给您带来错误,最糟糕的是,它会给您带来错误的变量!

为了解决该问题,您需要使它成为一个依赖表达式,最明显的方法是用data_替换所有this->data_等。

关于代码组织,将模板定义到单个头文件中;如果您真的想拆分成员实现,请将其放在具有合理文件扩展名的单个文件中(inl可以,cpp不能)...

10-07 12:41