2017-10-05 94 views
0

我有一个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; 
} */ 
+2

无关的注意:为什么你在头文件中包含.cpp文件?不要这样做 - 如果您想将多个.cpp文件编译为单个可执行文件,请首先创建目标文件并将它们链接起来以创建可执行文件。 –

回答

1

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

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

这意味着当编译器看到data_它认为它是位于其他地方的变量;充其量,它不会发现它给你一个错误,在最坏的情况下,它会给你一个错误的变量!

为了解决这个问题,你需要做的是一个依赖性表达,最显而易见的方法是更换所有data_this->data_,等...

关于你的代码组织,定义模板成单个头文件;如果你真的想拆分成员实现将它们放在一个合理的文件扩展名的单个文件(INL是好的,CPP不是)...

+0

好的,第一个错误解决了,谢谢。你提到一个'base_',但我不确定它在哪里。我继续并改变'resize'函数以包含'this->',并且还尝试了一个版本'BaseArray ::'。当他们都编译时,使用包含调用调整大小的主函数的驱动程序文件进行测试会给我一个分段错误。 – cparks10

+0

@ cparks10对不起Base_I我的意思是你的data_等.. –

+0

@ cparks10是的,你的调整大小的函数也有逻辑问题..但是,这是关闭的主题... –