May 14, 2009 at 5:35pm UTC
Greetings. I have a class that has a vector inside of it, and I am having trouble retrieving it after it has been set. I have boiled down the code to a source file tst.cpp, which includes a class definition file throwaway.h. Diagnostic output seems to indicate to me that the vector was properly set, but why am I getting size = 0 in Get?
Also, in the destructor, how do I delete the vector Vect? I can clear() it fine, but if I add the statement "delete Vect;" the code won't compile. Thanks.
Output
------
size = 3
1 1 1
In here
Still in here 3
Still still in here 3
OK
Field1_x = 3
Deg = 2
size = 0
Segmentation fault
tst.cpp
-------
#include <iostream>
#include <vector>
using namespace std;
#include "throwaway.h"
void showthr(throwaway<int> Thr);
int main()
{
int Deg;
int j;
int Field1_x;
vector<int> Vect1;
throwaway<int> Thr1;
//
Vect1.clear();
Field1_x = 3;
Deg = 2;
j = 1;
for (int i=0; i<=Deg; i++)
Vect1.push_back(j);
Thr1 = throwaway<int>(Field1_x, Deg, Vect1);
showthr(Thr1);
}
//--------------------------------------------------------------
void showthr(throwaway<int> Thr)
{
int Deg;
int Field1_x;
vector<int> Vect1;
//
cout << "OK" << endl;
Thr.Get(Field1_x, Deg, Vect1);
cout << endl
<< "Field1_x = " << Field1_x << " " << "Deg = " << Deg << endl;
for (int i=0; i<=Deg; i++)
cout << Vect1[i] << " ";
cout << endl;
}
throwaway.h
-----------
#include <vector>
template <typename X> class throwaway
{
public:
void Get(int &Field1_x, int &Deg, vector<X> &Thr);
const throwaway<X> operator=(const throwaway<X>& Thr2);
throwaway(const throwaway<X>& Thr2);
throwaway(X Coeff);
throwaway(int Field1_x, int Deg, const vector<X>& Thr);
throwaway();
~throwaway();
private:
int Field1;
int Degree;
vector<X> Vect;
};
//----------------------------------------------------------------------
template <typename X>
void throwaway<X>::Get(int &Field1_x, int &Deg, vector<X> &Thr)
{
Field1_x = Field1;
cout << "Field1_x = " << Field1_x << endl;
Deg = Degree;
cout << "Deg = " << Deg << endl;
cout << "size = " << Vect.size() << endl;
Thr.clear();
for (int i=0; i<=Degree; i++)
Thr.push_back(Vect[i]);
}
//----------------------------------------------------------------------
//
// Overloaded = operator.
//
template <typename X> const throwaway<X>
throwaway<X>::operator=(const throwaway<X>& Thr2)
{
cout << "In here" << endl;
if (this == &Thr2)
return *this;
cout << "Still in here " << Thr2.Vect.size() << endl;
Field1 = Thr2.Field1;
Degree = Thr2.Degree;
Vect.clear();
for (int i=0; i<=Degree; i++)
Vect.push_back(Thr2.Vect[i]);
cout << "Still still in here " << Vect.size() << endl;
return *this;
}
//----------------------------------------------------------------------
//
// Copy constructor.
//
template <typename X>
throwaway<X>::throwaway(const throwaway<X>& Thr2)
{
Field1 = Thr2.Field1;
Degree = Thr2.Degree;
vector<X> Vect(Thr2.Vect);
}
//----------------------------------------------------------------------
//
// Constructor.
//
template <typename X>
throwaway<X>::throwaway(int Field1_x, int Deg, const vector<X>& Thr)
:Field1(Field1_x), Degree(Deg), Vect(Thr)
{
cout << "size = " << Vect.size() << endl;
for (int i=0; i<Vect.size(); i++)
cout << Vect[i] << " ";
cout << endl;
}
//----------------------------------------------------------------------
//
// Default constructor (set to zero).
//
template <typename X> throwaway<X>::throwaway()
{
Field1 = 0;
Degree = 0;
X Zero = 0;
Vect.clear();
Vect.push_back(Zero);
}
//----------------------------------------------------------------------
//
// Destructor.
//
template <typename X> throwaway<X>::~throwaway()
{
Vect.clear();
}
May 14, 2009 at 5:58pm UTC
I don't see what the problem is. Get should get you a copy of the contents of the vector,
You don't need to call Clear in the destructor.
May 15, 2009 at 11:55am UTC
And at that point, the default member-wise copy copy constructor would suffice since the user-defined copy constructor does the same thing.