lundi 2 décembre 2019

double free or corruption (out) and Aborted (core dumped) error

Help, I'm new in programming and I'm also new in this site, pardon for my unorganized question. Also, I cannot understand the explanation for the same error from the other existing questions. I did not used malloc and did not have the same situation as the others. Lastly, this is one of our final requirements and I hope questions like this is valid to our prof.

#ifndef __VECTOR_H__
#define __VECTOR_H__

#include <iostream>
#include <fstream>
#include <vector>
#include <math.h>
using namespace std;

typedef unsigned int uint;

// Modified vector class to handle basic linear algebra operations.
template<typename T>
class Vector
{
    public:
        vector<T> elements;
        uint size;

        // Constructors
        Vector();
        Vector(uint);
        Vector(vector<T>);
        Vector(string);

        // Print vector elements.
        void print() const;

        // Save vector to a text file with string filename as parameter.
        void savetxt(string) const;

        // Append element after the last entry.
        void push_back(T value);

        // Check if vector has same size to other vector.
        bool has_equal_size(Vector<T>&) const;

        // Retuns dot product.
        double dot(Vector<T>&) const;

        // Returns Euclidean norm.
        double norm() const;

        // Indexing operator.
        T operator[] (uint);

        // Comparison operator.
        bool operator== (Vector<T>&);

        // Destructor.
        ~Vector();
};

// Default constructor
template<typename T>
Vector<T>::Vector() {};

// Constructor for zero vector with size equal to the parameter num_elements.
template<typename T>
Vector<T>::Vector(uint num_elements){
    for(uint i = 0; i <= num_elements; i++){
        elements.push_back(0);
    }
    size = num_elements;
}

// Constructor with vector data as parameter.
template<typename T>
Vector<T>::Vector(vector<T> data){
    for(uint i = 0; i <= data.size(); i++){
        elements.push_back(data[i]);
    }
    size = data.size();
}

// Constructor with txt filename as parameter.
/*
template<typename T>
Vector<T>::Vector(string filename){
    ifstream infile("filename.txt");
    if(infile.is_open()){
        while(infile >> value)
            V.push_back(value);
    }
    else{
        cout << "File does not exists!" << endl;
        exit(0);
    }
    infile.close();
}
*/
// Print vector elements.
template<typename T>
void Vector<T>::print() const{
    for(uint i = 0; i < size; i++){
            cout << elements[i] << " ";
    }
    cout << endl;
}
// Save vector elements to a txt file.
template<typename T>
void Vector<T>::savetxt(string filename) const{
    ofstream outfile("output.txt");
    if (outfile.is_open())
    {
        for (int i=0; i < size; i++){
            outfile << elements[i] << " ";
        }
    }
    else{
        cout << "File does not exists!" << endl;
        exit(0);
    }
    outfile.close();
}

// Check if vector has the same size as other parameter vector.
template<typename T>
bool Vector<T>::has_equal_size(Vector<T>& V) const{
    if(size == V.size)
        return true;

    else
        return false;
}

// Dot product.
template<typename T>
double Vector<T>::dot(Vector<T>& V) const{
    T dotproduct = 0;
    T temp;
    if (has_equal_size(V) == 0){
        cout << "Vectors must have equal size! ";
        return 0;
    }
    else{
        for(uint i=0; i < size; i++){
            temp = elements[i] * V.elements[i];
            dotproduct += temp;
        }
    }
    return dotproduct;
}

// Euclidean norm of vector.
template<typename T>
double Vector<T>::norm() const{
    double temp2;
    double sum;
    for(int i=0; i < size; i++){
        temp2 = elements[i]*elements[i];
        sum += temp2;
    }
    return sqrt(sum);
}

// Indexing operator.
template<typename T>
T Vector<T>::operator[] (uint index){
    return elements[index]; 
}

/*
Vector componentwise sum.
*/
template<typename T>
Vector<T> operator+ (Vector<T>& V, Vector<T>& W){
    Vector<T> S(0);
    double sum;
    if (V.has_equal_size(W) == 0){
        cout << "Vectors must have equal size! ";
    }
    else{
        for(uint i=0; i < V.size; i++){
            sum = V.elements[i] + W.elements[i];
            S.push_back(sum);
        }
    }
    return S;
}

template<typename T>
Vector<T> operator+ (Vector<T>& V, T scalar){
    Vector<T> S(0);
    double sum;
    for(uint i=0; i < V.size; i++){
            sum = V.elements[i] + scalar;
            S.push_back(sum);
        }
    return S;
}

template<typename T>
Vector<T> operator+ (T scalar, Vector<T>& V){
    Vector<T> S(0);
    double sum;
    for(uint i=0; i < V.size; i++){
            sum = scalar + V.elements[i];
            S.push_back(sum);
        }
    return S;
}

/*
Vector componentwise difference.
*/
template<typename T>
Vector<T> operator- (Vector<T>& V, Vector<T>& W){
    Vector<T> D(0);
    double diff;
    if (V.has_equal_size(W) == 0){
        cout << "Vectors must have equal size! ";
    }
    else{
        for(uint i=0; i < V.size; i++){
            diff = V.elements[i] - W.elements[i];
            D.push_back(diff);
        }
    }
    return D;
}

template<typename T>
Vector<T> operator- (Vector<T>& V, T scalar){
    Vector<T> D(0);
    double diff;
    for(uint i=0; i < V.size; i++){
            diff = V.elements[i] - scalar;
            D.push_back(diff);
        }
    return D;
}

template<typename T>
Vector<T> operator- (T scalar, Vector<T>& V){
    Vector<T> D(0);
    double diff;
    for(uint i=0; i < V.size; i++){
            diff = scalar - V.elements[i];
            D.push_back(diff);
        }
    return D;
}

/*
Vector componentwise multiplication.
*/
template<typename T>
Vector<T> operator* (Vector<T>& V, Vector<T>& W){
    Vector<T> P(0);
    double prod;
    if (V.has_equal_size(W) == 0){
        cout << "Vectors must have equal size! ";
    }
    else{
        for(uint i=0; i < V.size; i++){
            prod = V.elements[i] * W.elements[i];
            P.push_back(prod);
        }
    }
    return P;
}

template<typename T>
Vector<T> operator* (Vector<T>& V, T scalar){
    Vector<T> P(0);
    double prod;
    for(uint i=0; i < V.size; i++){
            prod = V.elements[i] * scalar;
            P.push_back(prod);
        }
    return P;
}

template<typename T>
Vector<T> operator* (T scalar, Vector<T>& V){
    Vector<T> P(0);
    double prod;
    for(uint i=0; i < V.size; i++){
            prod = scalar * V.elements[i];
            P.push_back(prod);
        }
    return P;
}

/*
Vector componentwise division.
*/
template<typename T>
Vector<T> operator/ (Vector<T>& V, Vector<T>& W){
    Vector<T> Q(0);
    double quot;
    if (V.has_equal_size(W) == 0){
        cout << "Vectors must have equal size! ";
    }
    else{
        for(uint i=0; i < V.size; i++){
            if(W.elements[i] = 0){
                cout << "Vector division not applicable! There should be no component(s) with zero value in the second vector." << endl;
            }
            else{
                quot = V.elements[i] / W.elements[i];
                Q.push_back(quot);
            }
        }
    }
    return Q;
}

template<typename T>
Vector<T> operator/ (Vector<T>& V, T scalar){
    Vector<T> Q(0);
    double quot;
    for(uint i=0; i < V.size; i++){
        if(scalar = 0){
            cout << "Vector division not applicable! Scalar value must not be equal to zero." << endl;
        }
        else{
            quot = V.elements[i] / scalar;
            Q.push_back(quot);
        }
    }
    return Q;
}

template<typename T>
void operator/ (T scalar, Vector<T>& V){
    cout << "\nError: Operation not supported!" << endl;
}

// Vector push_back version.
template<typename T>
void Vector<T>::push_back(T value){
    elements[size] = value;
    size++;
}

// Comparison ovalue.;
template<typename T>
bool Vector<T>::operator== (Vector<T>& V){
    uint truth;
    if (has_equal_size(V) == 0){
        return false;
    }
    else{
        for(uint i=0; i < V.size; i++){
            if(elements[i] == V.elements){
                truth = 1;  
            }
            else{
                return false;
                break;
            }
        }
        if(truth == 1)
            return true;
        else
            return false;
    }
}

// Default Destructor.
template<typename T>
Vector<T>::~Vector() {};

#endif

Aucun commentaire:

Enregistrer un commentaire