Hi

I am having issues populating a vector using "new" where nodeVec is a vector where each entry consists of another vector with 3 elements. zMap is the vector I am trying to populate with the sum of the squares of the first two elements for each entry in nodeVec.

Any help will be greatly appreciated!

vector <double> mapNodalData(vector <Vec*> nodeVec, vector <double>& zMap){

    for (unsigned int i = 0; i < nodeVec.size(); i++){
        double vector *pz = new double vector(pow(((nodeVec[i])[0]),2) + pow(((nodeVec[i])[1]),2));
        zMap.push_back(pz);
        pz = NULL;
    }
    return zMap;
}

Hi ollie60 :-)

There is no need to allocate any dynamic memory like that.

You don't need to allocate a new vector. All you have to do is calculate the sum of the values from nodeVec and push it to the back of zMap:

vector <double> mapNodalData(vector <Vec*> nodeVec, vector <double>& zMap){

    for (unsigned int i = 0; i < nodeVec.size(); i++){
        double sum = pow(((nodeVec[i])[0]),2) + pow(((nodeVec[i])[1]),2);
        zMap.push_back(sum);
    }
    return zMap;
}

NOTE: Untested code.

Edited 6 Years Ago by mbulow: n/a

When do you have to use dynamic memory then? I know that it is when you dont know the size of an object at compile time and in this code I am reading in a file that produces nodeVec.

Also I tried the code and I get an error:
error: cannot convert 'Vec' to 'double' for argument '1' to 'double pow(double, double)'

But I should add that Vec is a 3D vector class that I have created.

You are right about the fact that you use dynamic memory allocation when you don't know the size at compile-time. But a vector is like a dynamic array. It takes care of the memory allocation for you.

Storing 10 or 100 values in a vector makes no difference. The vector will grow as you need it.

Regarding the compile-error: Could you please post the code for your Vec-class.

Thanks for the help.

Here is the code for my Vec class

///////////////////////////////////////
/// \file vec.cc
/// \brief source file for the 3D vector class
///////////////////////////////////////

#include "vec.h"

///overloaded friend operators
///
/// Method:
/// -Display a vector of type Vec as it would be displayed with standard built in types
/// -The x, y, z components are sent to the screen with a tab separating them
/// \param ostream& (lhs)
/// \param const Vec& (rhs)
/// \return

ostream & operator << (ostream& out, const Vec& inVec){
    out  << "\t" << inVec.x << "\t" << inVec.y << "\t" << inVec.z;
    return out;
}

/// Method:
/// -Reads in a vector of type Vec as it would be done using standard built in types
/// -Reads in the x, y, z components of the vector
/// -Checks that the input is acceptable
/// -If the input is not acceptable:
///  -Display an error message
///  -return the default value of Vec
/// \param istream& (lhs)
/// \param Vec& (rhs)
/// \return

istream& operator >> (istream& in, Vec& inVec){
    in >> inVec.x >> inVec.y >> inVec.z;
    if (!in){
        cout << "Error reading data for Vec." << endl;
        inVec = Vec();
    }
    return in;
}

/// Method:
/// -Takes in two vectors to be compared
/// -Return true if x, y, z vomponents are respectively equal
/// -Returns false otherwise
/// \param const Vec& (lhs)
/// \param const Vec& (rhs)
/// \return true or false

bool operator == (const Vec& lhs, const Vec& rhs){
    return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
}

/// Method:
/// -Takes in two vectors to be compared
/// -Compares the two vectors using the overloaded == operator
/// -Returns true if they are not equal
/// -Returns false otherwise
/// \param const Vec& (lhs)
/// \param consr Vec& (rhs)
/// \return true or false

bool operator != (const Vec& lhs, const Vec& rhs){
    return !(lhs == rhs);
}

/// Method:
/// -Takes in two vectors to be added together
/// -Uses compound addition to add the vectors together
/// \param const Vec&
/// \param const Vec&
/// \return the vector resulting from addition of the two input vectors together

Vec operator + (const Vec& lhs, const Vec& rhs){
    Vec theVec(lhs);
    theVec += rhs;
    return theVec;
}

/// Method:
/// -Takes in two vectors to be subtracted from one another
/// -Uses compound subtraction to subtract the vectors
/// \param const Vec&
/// \param const Vec&
/// \return the vector resulting from subtracting the two input vectors

Vec operator - (const Vec& lhs, const Vec& rhs) {
    Vec theVec(lhs);
    theVec -= rhs;
    return theVec;
}

//overloaded operators

/// Method:
/// -Overloads the = operator
/// -LHS vector is implicitly passed, rhs is passed as const Vec&
/// -Sets the x, y, z component of the lhs vector to that of the rhs vector
/// \param const Vec&
/// \return the lhs vector with the same components as the rhs vector

Vec& Vec::operator = (const Vec& rhs){
    x = rhs.x;
    y = rhs.y;
    z = rhs.z;
    return *this;
}

/// Method:
/// -Overload the += operator
/// -LHS vector is implicitly passed, rhs is passed as const Vec&
/// -Adds the x component of rhs vector the x component of lhs vector
/// -Adds the y component of rhs vector the y component of lhs vector
/// -Adds the z component of rhs vector the z component of lhs vector
/// \param const Vec&
/// \return the modified lhs vector

Vec& Vec::operator +=(const Vec& rhs){
    x += rhs.x;
    y += rhs.y;
    z += rhs.z;
    return *this;
}

/// Method:
/// -Overload the -= operator
/// -Lhs vector is implicitly passed, rhs is passed as const Vec&
/// -Subtracts the x component of rhs vector the x component of lhs vector
/// -Subtracts the y component of rhs vector the y component of lhs vector
/// -Subtracts the z component of rhs vector the z component of lhs vector
/// \param const Vec&
/// \return the modified lhs vector

Vec& Vec::operator -=(const Vec& rhs){
    x -= rhs.x;
    y -= rhs.y;
    z -= rhs.z;
    return *this;
}

/// Method:
/// -Overload the [] operator
/// -Takes in an integer as the rhs argument
/// -Lhs vector is passed implicitly
/// -If the integer is 0, the x component of the lhs vector is returned
/// -If the integer is 1, the y component of the lhs vector is returned
/// -If the integer is 2, the z component of the lhs vector is returned
/// -If the integers vaule is other than 0, 1, 2 an error message is displayed
/// \param const int
/// \return the appropriate vector component of the lhs vector

double& Vec::operator [](const int n){
    switch (n){
        case(0) : return x;
            break;
        case(1) : return y;
            break;
        case(2) : return z;
            break;
        default : cerr << "Error: Index exceeds bounds (0-2)\n";
            break;
    }
}

/// Method:
/// -Overload the * operator
/// -Take in the lhs vector impicitly and a scalar, (rhs)
/// -Multiply each component of the lhs vector with the scalar
/// \param const double
/// \return a vector with the components of the lhs vector multiplied by a scalar

Vec Vec::operator *(const double rhs){
    Vec v;
    v.x = x*rhs;
    v.y = y*rhs;
    v.z = z*rhs;
    return v;
}

/// Method:
/// -Overload the * operator
/// -Take in the lhs vector implicitly and a rhs vector as a const Vec&
/// \param const vec&
/// \return the cross product

Vec Vec::operator *(const Vec& rhs){
    Vec v;
    v.x = y*rhs.z-z*rhs.y;
    v.y = x*rhs.z-z*rhs.x;
    v.z = x*rhs.y-y*rhs.x;
    return v;
}

/// Method:
/// -Overload the ^ operator
/// -Takes in the lhs vector implicitly and a rhs vector as const Vec&
/// \param const Vec&
/// \return the value of the dot product

double Vec::operator ^(const Vec& rhs){
    return (x*rhs.x + y*rhs.y + z*rhs.z);
}

//Methods

/// Method:
/// -Takes in nothing and returns the length of the vector
/// \param void
/// \return length of the vector

double Vec::norm(void) const {
    return sqrt(pow(x,2) + pow(y,2) + pow(z,2));
}

/// Method:
/// -Takes in nothing
/// -Sets the max value to the x component
/// -If y, z components are greater than the max value, the max value is set to respective value
/// \param void
/// \return max component of the vector

double Vec::max(void){
    double max = x;
    if (y > max){
        max = y;
    }
    if (z > max){
        max = z;
    }
    return max;
}

/// Method:
/// -Takes in nothing
/// -Sets the min value to the x component
/// -If y, z components are smaller than the min value, the min value is set to respective value
/// \param void
/// \return min component of the vector

double Vec::min(void){
    double min = x;
    if (y < min){
        min = y;
    }
    if (z < min){
        min = z;
    }
    return min;

Ahh... Now I see why you get the compile error. At first I suspected that you had forgotten to implement the []-operator but the compiler fails because nodeVec is of type vector<Vec*>.

Because your using Vec-pointers you have to dereference the pointer before using the []-operator. Like this:

vector <double> mapNodalData(vector <Vec*> nodeVec, vector <double>& zMap){

    for (unsigned int i = 0; i < nodeVec.size(); i++){
        double sum = pow((*nodeVec[i])[0], 2) + pow((*nodeVec[i])[1], 2);
        zMap.push_back(sum);

    }
    return zMap;
}

Ok I think I get it. I am dereferencing it because I then obtain the value.

Thanks a lot it works!

Edited 6 Years Ago by ollie60: n/a

This question has already been answered. Start a new discussion instead.