因此,我創建了一個名為 unorderedSet 的派生類模板,它使用一個指標成員變數來保存堆上陣列的位置(使用 new)。
我試圖多載算術運算子以找到兩個不同的 unorderedSet 物件的交集和并集,但我的代碼在第一個多載函式呼叫結束時終止,并顯示一條警告說“對與區域變數 'unSet' 關聯的堆疊記憶體的參考”回傳 [-Wreturn-stack-address]。” 我將在下面發布我的代碼的相關部分。
template <class elemType>
class unorderedSet: public unorderedArrayListType<elemType>
{
public:
void insertAt(int location, const elemType& insertItem);
void insertEnd(const elemType& insertItem);
void replaceAt(int location, const elemType& repItem);
const unorderedSet<elemType>& operator (const unorderedSet<elemType>&);
// Function to overload the binary operator to find the union of a pair/group of sets
// Postcondition: Finds the union of the sets
const unorderedSet<elemType>& operator-(const unorderedSet<elemType>&);
// Function to overload the binary operator - to find the intersection of a pair/group of sets
// Postcondition: Finds the intersection of the sets
unorderedSet(int size = 100);
unorderedSet(const unorderedSet<elemType>& otherSet);
~unorderedSet();
protected:
elemType *set;
int length;
int maxSize;
};
template <class elemType>
const unorderedSet<elemType>& unorderedSet<elemType>::operator (const unorderedSet<elemType>& otherSet)
{
unorderedSet<elemType> unSet(this->length otherSet.length); // Initializes new set to hold values of the union set
for (int i = 0; i < this->length; i )
unSet.insertEnd(this->list[i]); // Assigns all values of the activating operand to the union set using insertEnd
for (int i = 0; i < otherSet.length; i )
unSet.insertEnd(otherSet.list[i]); // Calls insertEnd() to both check for duplicate values and add unique values to the union of the sets
cout << "\n\nunSet:\n";
unSet.print(); // Checks the values of the union set
//This is the last block of code my program runs that I know of
return unSet; // Should return the union set, but dumps the core at the moment
} // end operator overload
template <class elemType>
unorderedSet<elemType>::unorderedSet(int size) : unorderedArrayListType<elemType>(size)
{
if (size <= 0)
{
cout << "The array size must be positive. Creating an array of the size 100. " << endl;
this->maxSize = 100;
}
else
this->maxSize = size;
this->length = 0;
set = new elemType[this->maxSize];
}
template <class elemType>
unorderedSet<elemType>::~unorderedSet()
{
delete [] set;
}
template <class elemType>
unorderedSet<elemType>::unorderedSet(const unorderedSet<elemType>& otherSet)
{
this->maxSize = otherSet.maxSize;
this->length = otherSet.length;
set = new elemType[this->maxSize];
for (int j = 0; j < length; j )
set[j] = otherSet.set[j];
}
回傳值之前的 cout 陳述句允許我檢查該函式在該點之前是否在執行其作業,并且到目前為止,它都按預期作業。
以下代碼來自我的測驗客戶端程式。
int main()
{
int intArr1[6] = {0, 1, 2, 3, 4, 5};
unorderedSet<int> testIntSet1;
for (int i = 0; i < (sizeof(intArr1) / sizeof(intArr1[0])); i )
testIntSet1.insertEnd(intArr1[i]);
// Some more code before the function call
int intArr2[6] = {0, 1, 3, 6, 7, 9};
unorderedSet<int> testIntSet2, testIntSet3;
for (int i = 0; i < (sizeof(intArr2) / sizeof(intArr2[0])); i )
testIntSet2.insertEnd(intArr2[i]);
testIntSet3 = testIntSet1 testIntSet2;
// Some more code
}
客戶端程式中的最后一行之后沒有任何內容運行,所以知道這就是問題所在。我正在努力解決的是如何修復我的運算子多載函式,以便回傳值不會回傳對函式結束后銷毀的記憶體地址的參考。我在這個網站上閱讀了很多帖子,其中用戶經常建議使用 malloc 或 calloc,但我不確定這是我需要做的,所以我想我會發布一些代碼并詢問你們。
uj5u.com熱心網友回復:
問題是您正在回傳對堆疊物件的參考,其生命周期是operator 函式的范圍。
template <class elemType>
const unorderedSet<elemType>& unorderedSet<elemType>::operator (const unorderedSet<elemType>& otherSet)
請注意,您正在回傳一個const unorderedSet<elemType>&對集合的參考。
您所要做的就是回傳一個物件,該物件將被復制到目標變數(盡管編譯器很可能會使用 RVO 對其進行優化,從而消除復制)
所有這一切都需要將回傳型別更改為物件,而不是參考:
template <class elemType>
const unorderedSet<elemType> unorderedSet<elemType>::operator (const unorderedSet<elemType>& otherSet)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321226.html
標籤:C
