2017-10-05 11 views
0

BaseArray classを継承するArray classがあります。 BaseArrayには、保護されたメンバー変数data_cur_size_があります。 Arrayクラスはresize関数を導入しています。私が遭遇している問題は、BaseArrayの保護されたメンバ変数のどれもがresize関数でアクセスされているように見えないということです。考えられる継承スコープ問題:

EDIT: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つの実行可能ファイルにコンパイルする場合は、まずオブジェクトファイルを作成し、それらをリンクして実行可能ファイルを作成します。 –

答えて

1

最初のエラーに関しては、Arrayに宣言されたmax_size()メンバはありません。

第2のエラーに関しては、テンプレート内の名前検索は、非依存式が定義ポイントで参照される2段階ロジックに従いますが、依存式はインスタンス化ポイントで参照されます。

これは、コンパイラがdata_を見ると、それが別の場所にある変数だと考えられることを意味します。せいぜい、それはあなたにエラーを与えるのを見つけられません、最悪の場合、あなたに間違った変数を与えるでしょう!

問題を解決するために、あなたはなど依存性発現、this->data_ですべてdata_を交換されている最も明白な方法、ことを確認する必要があり...

あなたのコードの組織については、にあなたのテンプレートを定義単一のヘッダファイル。あなたが実際にメンバの実装を分割したい場合は、ファイル拡張子を付けて1つのファイルに配置してください(inlは大丈夫です、cppはありません)...

+0

さて、最初のエラーが解決しました、ありがとうございます。あなたは 'base_'と言いますが、どこにいるのか分かりません。私は先に進み、 'this->'をインクルードする 'resize'関数を変更し、' BaseArray :: 'でバージョンを試しました。どちらもコンパイルしていますが、リサイズを呼び出す主な関数を含むドライバファイルでテストすると、私にセグメンテーションフォールトが与えられます。 – cparks10

+0

@ cparks10あなたのデータ_などを意味していました。 –

+0

@ cparks10はい、あなたのサイズ変更機能にも論理的な問題がありますが、これは話題にはなりません... –