簡體   English   中英

模板類之間的可見性

[英]visibility among templated classes

顧名思義,我有兩個類TwoWayVector和TwoWayVectorIterator,我正在嘗試實現自己的向量類和一個迭代器。 我似乎遇到了一些可見性問題,並且我也不確定如何從TwoWayVector.begin()方法構造一個TwoWayVectorIterator。

TwoWayVector.cc

#include <sstream>

using namespace std;

template <class T> class TwoWayVector{
public:

T* data;
int capacity;
int nextFree;

TwoWayVector(){
    capacity = 10;
    nextFree = 0;
    data = new T[capacity];
}

~TwoWayVector(){
    delete data;
}

T& operator[](const int index){
    if( index >= capacity || capacity + index < 0){
        string number = static_cast<ostringstream*>( &(ostringstream() << index) )->str();
        string error = "index " + number + " is out of bounds"; 
        throw error;
    }
    else if(index < 0){
        return data[nextFree+index];
    }
    return  data[index];
}
 //memory leaks?
void push_back(T object){
    if(capacity <= nextFree){
        capacity = capacity*2;
        T* tmp = new T[capacity];
        for(int i=0; i<capacity; i++){
            tmp[i] = data[i];
        }
        delete data;
        data = tmp;
    }
    data[nextFree] = object;
    nextFree++;
}

T pop_back(){
    nextFree--;
    T result = data[nextFree];
    data[nextFree] = NULL; 
    return result;
}

int size(){
    return nextFree;
}

TwoWayVectorIterator begin(){
    TwoWayVectorIterator<T> iterator= new TwoWayVectorIterator<T>(0,this);
    return (iterator);
}

};

TwoWayVectorIterator.cc

using namespace std;

template <class T> class TwoWayVectorIterator{
public:
TwoWayVector<T>* vector;
int currentPosition;
TwoWayVectorIterator(TwoWayVector<T>& vec){
    currentPosition = 0;
    vector = vec;
}
TwoWayVectorIterator( int pos , TwoWayVector<T>& vec){
    currentPosition = pos;
    vector = vec;
}

bool& operator==(const TwoWayVectorIterator* vector2){
    bool address, position;
    address = (&vector == &vector2) ? true : false;
    position =(currentPosition == vector2->currentPosition) ? true : false;
    return (address && position);
}

bool& operator!=(const TwoWayVectorIterator* vector2){
    bool address, position;
    address = (&vector == &vector2) ? true : false;
    position=(currentPosition == vector2->currentPosition) ? true : false;
    return (address && position);
}

TwoWayVectorIterator& operator++(){
    currentPosition = (currentPosition+1);
    return *this;
}
TwoWayVectorIterator& operator++(int){
    currentPosition = (currentPosition+1);
    return *this;
}
TwoWayVectorIterator& operator=(TwoWayVectorIterator* vector2){
    &vector = vector2;
    currentPosition = vector2->currentPosition;
    return *this;
}
TwoWayVectorIterator& operator+(int n){
    currentPosition = currentPosition+n;
    return *this;
}
TwoWayVectorIterator& operator-(int n){
    currentPosition = currentPosition-n;
    return *this;
}
bool& operator<(TwoWayVectorIterator* vector2){
    return (currentPosition<vector2->currentPosition);
}
T& operator*(){
    return vector[currentPosition];
}
};

從Test.cc調用

using namespace std;
#include <iostream>
#include "TwoWayVector.cc"
#include "TwoWayVectorIterator.cc"

int main(){

TwoWayVector<int> numbers;
numbers.push_back(3);
numbers.push_back(2);
numbers.size();
TwoWayVectorIterator current = numbers.begin();
return 0;

}

編譯器錯誤:

In file included from Test.cc:3:
TwoWayVector.cc:59: error: ‘TwoWayVectorIterator’ does not name a type
Test.cc: In function ‘int main()’:
Test.cc:18: error: missing template arguments before ‘current’
Test.cc:18: error: expected `;' before ‘current’

我已經嘗試過聲明兩種不同的方式,不同的包含方案,並調用TwoWayVectorIterator current = numbers.begin(),但是我不想指定迭代器的類型。

非常感謝您的任何幫助!!

第一個問題:

似乎您沒有#include包含文件TwoWayVector.cc包含TwoWayVectorIterator定義的文件,該文件在函數begin()使用其定義。

嘗試添加以下指令:

    #include <sstream>
    #include "TwoWayVectorIterator.cc"
^^  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

第二個問題:

TwoWayVectorIterator.cc ,您將必須提交一個前向聲明以使其意識到TwoWayVector的存在:

template<typename T> class TwoWayVector;

第三個問題:

另外,在函數begin()您使用TwoWayVectorIterator作為返回類型,而沒有指定必要的模板參數:

TwoWayVectorIterator<T> begin(){
//                  ^^^
    TwoWayVectorIterator<T> iterator= new TwoWayVectorIterator<T>(0,this);
    return (iterator);
}

第四個問題:

您的main()函數遇到類似的問題:

int main()
{
    TwoWayVector<int> numbers;
    // ...
    TwoWayVectorIterator<int> current = numbers.begin();
    //                  ^^^^^
    // ...
}

第五題:

另一個問題是,構造TwoWayVectorIterator應該接受一個指針 ,而不是一個參考, TwoWayVector (至少從你如何使用它判斷):

TwoWayVectorIterator( int pos , TwoWayVector<T>* vec){
//                              ^^^^^^^^^^^^^^^^
    currentPosition = pos;
    vector = vec;
}

第六題:

operator ==operator !=重載正在返回臨時bool對象的引用,引用最終將在函數返回時被銷毀,從而給您留下了懸空的引用,並在嘗試取消引用它時導致未定義行為。

您應該只返回bool

    bool operator==(const TwoWayVectorIterator* vector2){
//  ^^^^ 
//  Return by value here!

        // ...
        return (address && position);
    }

    bool operator!=(const TwoWayVectorIterator* vector2){
//  ^^^^
//  Return by value here!

        // ...
        return (address && position);
    }

相關建議:

此外,應避免在全局名稱空間范圍內using此類using指令:

using namespace std;

這會將所有名稱從std命名空間導入到全局命名空間,這可能導致不希望的名稱沖突。

作為進一步的一般建議,請避免為變量提供標准容器類的名稱(例如vector )。 選擇像myVector ,或innerVector ,或任何你認為最適合作為一個描述性的名稱。

此外,您應該遵循命名約定,並分別給您的頭文件和實現文件擴展名,例如.h (或.hpp )和.cpp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM