Segmentation fault in Copy creator

Hello all.

I have a weird problem with a copy creator. I'm sure that is something stupid but I can't understand why is working in that way. Lets say that I have a Class Vector that stores the length of the vector and its data, and another class that inherits from it that has a member of Vector class (in the example code I have renamed the templated vector to RVec = Vector<double>)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <typename T>
class Vector{   
/*  friend std::ostream& operator<<(std::ostream& output, Vector &v);*/
  public:
    Vector(){};                                         // General creator 
    Vector(int nin);                                    // Creator of a vector of size n with zeros
    Vector(const Vector<T> &v);                       // Copy constructor
    ~Vector(){};  
    void Show();
  private:
    int n;
    T* x;
};
typedef Vector<double> RVec ;

class SEM{
  public:
    SEM(){};
    SEM(int N);  // Creator 
    ~SEM(){};   
    void Show();
  private:
   RVec r;  
};


In the creator of the second class I have to define a temporary copy and then assign to the member r of the SEM class. I'm sure that there is a more elegant way of doing but I don't know how to do it. Any tips?
1
2
3
4
5
6
7
// SEM methods
SEM::SEM(int N){
  RVec rt(N);
  rt.Show();
  r=rt;
  r.Show();  
}


I have had a lot of issues until I realised that the problem was my overloading = costructor. I have something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T>
Vector<T>& Vector<T>::operator=(const Vector<T> &v){
  if (this == &v){      // Same object?
      return *this;}
  else{
      Vector result(*this);
      std::cout << "Overloaded = " << std::endl; 
      result.ShowValues();
  for (int i=0; i<n; i++){
    x[i] = v.x[i];
  }
  return *this;
  }  
}

and when defining such operator (that is bassically a copy of the copy constructor) I get always a segmentation fault. Why?

Thanks in advance
Does the copy constructor by any chance call operator=()?
Topic archived. No new replies allowed.