Passing a vector<customStructure> to functions outside main()

A

{AGUT2} {H}-IWIK

Hi,

I'm trying to pass my vector to a function external to my main(), but my
compiler is complaining. FYI, the struct for the facetInfo works perfectly,
it's just the vector passing.

A quick overview... I have four sets of co-ordinates in each 'facetInfo',
and a vector of 'facetInfo's. I want to strip those 'facetInfo' objects
that have y-values of zero (.v1y, .v2y and .v3y), and return a 'facetInfo'
vector with the remaining 'facetInfo's into my main().

Here's a stripped down version....

****** Begin Code Snippet ******

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;


// Creates the basic framework for the facets to be read in
struct facetInfo {
double v1x;
double v1y;
double v1z;
double v2x;
double v2y;
double v2z;
double v3x;
double v3y;
double v3z;
double nox;
double noy;
double noz;
}; // Semicolon necessary

int main()
{
vector<facetInfo> mySurface;
facetInfo holdMe; // Holder facet to allow push_back() function.

ifstream inf("input.stl");
char buffer[128]; // File input buffer - 128 letter maximum

while(!inf.getline(buffer, 128, ' ').eof())
{
... // fills mySurface with 'mySurface.push_back(holdMe);'s
}
}

vector<facetInfo> cutZeros(vector<facetInfo> mainHull)
{
facetInfo holder;
int goFor = mainHull.size();
int atNum = 0;
vector<facetInfo> newHull;
while (atNum<goFor)
{
if (mainHull.v1y != 0)
{
if (mainHull.v2y != 0)
{
if(mainHull.v3y != 0)
{
holder.v1x = mainHull.v1x;
holder.v1y = mainHull.v1y;
holder.v1z = mainHull.v1z;
holder.v2x = mainHull.v2x;
holder.v2y = mainHull.v2y;
holder.v2z = mainHull.v2z;
holder.v3x = mainHull.v3x;
holder.v3y = mainHull.v3y;
holder.v3z = mainHull.v3z;
holder.nox = mainHull.nox;
holder.noy = mainHull.noy;
holder.noz = mainHull.noz;
newHull.push_back(holder);
} // end3 if
} // end2 if
} // end1 if
atNum++;
} // endwhile
return newHull;
}

****** End Code Snippet ******


The compiler says 'v1x' [and all the others] is not a member of
'vector<facetInfo,allocator<facetInfo> >' in function
cutZeros(vector<facetInfo,allocator<facetInfo>>)

Now, I read something about changing the cutZeros function to:


****** Begin Code Snippet ******
// line into main() after teh while loop
vector<facetInfo>& facinf = mySurface;
// out of main()


vector<facetInfo> cutZeros(const facetInfo& f)
{
facetInfo holder;
int goFor = mainHull.size();
int atNum = 0;
vector<facetInfo> newHull;
...
return newHull;
}

****** End Code Snippet ******


But the compiler says that 'size' is not a member of 'facetInfo' in
function 'cutZeros(const facetInfo &)'

Am I able to do this, or am I living in false hope?

Thank you again for your help, c.l.c++ - When I have a modicum of skill I
will pay your kindness back :)

Alex Livingstone
 
R

Ron Natalie

{AGUT2} {H}-IWIK said:
holder.v1x = mainHull.v1x;

mainHull isn't a facetinfo, it's a vector<facitinfo>

If you want to extract atNum's entry you need to do

holder.v1x = mainHull[atNum].v1x;

Actually, there's no reason to individually copy each member.

holder = mainHull[atNum];

will work just fine.
 
A

{AGUT2} {H}-IWIK

mainHull isn't a facetinfo, it's a vector<facitinfo>

Thank you. Consider me officially slapped 'upside the head'.

I apologise for the silly question.

Alex.
 
R

Rob Williscroft

{AGUT2} {H}-IWIK wrote in
Hi,

I'm trying to pass my vector to a function external to my main(), but
my compiler is complaining. FYI, the struct for the facetInfo works
perfectly, it's just the vector passing.
[snip]

vector<facetInfo> cutZeros(vector<facetInfo> mainHull)
{

Note in this case:

vector<facetInfo> cutZeros(vector<facetInfo> const & mainHull)
{

Would be more eficient, as you don't modify mainHull.
facetInfo holder;
int goFor = mainHull.size();
int atNum = 0;
vector<facetInfo> newHull;
while (atNum<goFor)
{

simplest option is to change mainHull.v1y etc below to
mainHull[ atNum ].v1y.

vector< facetInfo > is an indexable container of facetInfo's its
the [ atNum ] that selects the item.

if (mainHull.v1y != 0)
{
if (mainHull.v2y != 0)
{
if(mainHull.v3y != 0)
{
holder.v1x = mainHull.v1x;
holder.v1y = mainHull.v1y;
holder.v1z = mainHull.v1z;
holder.v2x = mainHull.v2x;
holder.v2y = mainHull.v2y;
holder.v2z = mainHull.v2z;
holder.v3x = mainHull.v3x;
holder.v3y = mainHull.v3y;
holder.v3z = mainHull.v3z;
holder.nox = mainHull.nox;
holder.noy = mainHull.noy;
holder.noz = mainHull.noz;
newHull.push_back(holder);
} // end3 if
} // end2 if
} // end1 if
atNum++;
} // endwhil
return newHull;
}

****** End Code Snippet ******

[snip]

HTH

Rob.
 
C

Chris Theis

{AGUT2} {H}-IWIK said:
Thank you. Consider me officially slapped 'upside the head'.

I apologise for the silly question.

Alex.

Just some other comments. You should consider implementing a 3D vector
class/struct which will make your code more legible:

struct facetInfo {
CVector m_Vertex1;
CVector m_Vertex2;
CVector m_Vertex3;
CVector m_Normal;

}; // Semicolon necessary

A simple implementation of a 3D vector coud look like this:

class CVector
{
public :
CVector() : X(0), Y(0), Z(0) {}
CVector ( double x, double y, double z ) : X(x), Y(y), Z(z) { }
CVector (const CVector& v) { X = v.X; Y = v.Y; Z = v.Z; }
virtual ~CVector () {}

CVector operator + (const CVector& v ) const { return CVector( X + v.X, Y
+ v.Y, Z + v.Z ); }
CVector operator - (const CVector& v ) const { return CVector( X - v.X,
Y - v.Y, Z - v.Z ); }
CVector operator -() const { return CVector( -X, -Y, -Z ); }
CVector operator / (const double Denominator) const { return CVector( X /
Denominator, Y / Denominator, Z / Denominator ); }
CVector operator / (const CVector& Denominator) const { return CVector( X
/ Denominator.X, Y / Denominator.Y, Z / Denominator.Z ); }
CVector operator * (const double Multiplicator) const { return CVector( X
* Multiplicator, Y * Multiplicator, Z * Multiplicator ); }
CVector operator * (const CVector& v) const { return CVector( X * v.X, Y *
v.Y, Z * v.Z ); }
bool operator == (const CVector& v) const { return (fabs(X - v.X) <
EPSILON) && (fabs(Y - v.Y) < EPSILON) && (fabs(Z - v.Z) < EPSILON); }
bool operator != (const CVector& v) const { return ! (*this == v); }
const CVector& operator += (const CVector& v) { X += v.X; Y += v.Y; Z +=
v.Z; return *this; }
const CVector& operator -= (const CVector& v) { X -= v.X; Y -= v.Y; Z -=
v.Z; return *this; }
const CVector& operator *= (const CVector& v) { X *= v.X; Y *= v.Y; Z *=
v.Z; return *this; }
const CVector& operator *= (const double m) { X *= m; Y *= m; Z *= m;
return *this; }
const CVector& operator /= (const CVector& v) { X /= v.X; Y /= v.Y; Z /=
v.Z; return *this; }
const CVector& operator /= (const double d) { X /= d; Y /= d; Z /= d;
return *this; }
CVector CrossProd( const CVector& v ) const { return CVector( Y * v.Z - Z
* v.Y, Z * v.X - X * v.Z, X * v.Y - Y * v.X ); }
double DotProd( const CVector& v ) const { return X * v.X + Y * v.Y + Z *
v.Z; }
void Set( double x, double y, double z ) { X = x; Y = y; Z = z; }
void Get( double& x, double& y, double& z ) const { x = X; y = Y; z = Z; }
double Norm() const { return (double)( X*X ) + (double)( Y*Y ) +
(double)(Z*Z);}
double Length() const { return sqrt( Norm() ); }
double Distance( const CVector& v ) const { return (v - *this).Length(); }
CVector Min( const CVector& v ) const { return CVector( X < v.X ? X : v.X,
Y < v.Y ? Y : v.Y, Z < v.Z ? Z : v.Z ); };
CVector Max( const CVector& v ) const { return CVector( X > v.X ? X : v.X,
Y > v.Y ? Y : v.Y, Z > v.Z ? Z : v.Z ); };

double X;
double Y;
double Z;
};

HTH
Chris
 
L

Loran Hayden

mainHull is a vector, not a facetInfo struct. that might clear up anything
else. (and why are you passing by value instead of reference?)

{AGUT2} {H}-IWIK said:
Hi,

I'm trying to pass my vector to a function external to my main(), but my
compiler is complaining. FYI, the struct for the facetInfo works perfectly,
it's just the vector passing.

A quick overview... I have four sets of co-ordinates in each 'facetInfo',
and a vector of 'facetInfo's. I want to strip those 'facetInfo' objects
that have y-values of zero (.v1y, .v2y and .v3y), and return a 'facetInfo'
vector with the remaining 'facetInfo's into my main().

Here's a stripped down version....

****** Begin Code Snippet ******

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;


// Creates the basic framework for the facets to be read in
struct facetInfo {
double v1x;
double v1y;
double v1z;
double v2x;
double v2y;
double v2z;
double v3x;
double v3y;
double v3z;
double nox;
double noy;
double noz;
}; // Semicolon necessary

int main()
{
vector<facetInfo> mySurface;
facetInfo holdMe; // Holder facet to allow push_back() function.

ifstream inf("input.stl");
char buffer[128]; // File input buffer - 128 letter maximum

while(!inf.getline(buffer, 128, ' ').eof())
{
... // fills mySurface with 'mySurface.push_back(holdMe);'s
}
}

vector<facetInfo> cutZeros(vector<facetInfo> mainHull)
{
facetInfo holder;
int goFor = mainHull.size();
int atNum = 0;
vector<facetInfo> newHull;
while (atNum<goFor)
{
if (mainHull.v1y != 0)
{
if (mainHull.v2y != 0)
{
if(mainHull.v3y != 0)
{
holder.v1x = mainHull.v1x;
holder.v1y = mainHull.v1y;
holder.v1z = mainHull.v1z;
holder.v2x = mainHull.v2x;
holder.v2y = mainHull.v2y;
holder.v2z = mainHull.v2z;
holder.v3x = mainHull.v3x;
holder.v3y = mainHull.v3y;
holder.v3z = mainHull.v3z;
holder.nox = mainHull.nox;
holder.noy = mainHull.noy;
holder.noz = mainHull.noz;
newHull.push_back(holder);
} // end3 if
} // end2 if
} // end1 if
atNum++;
} // endwhile
return newHull;
}

****** End Code Snippet ******


The compiler says 'v1x' [and all the others] is not a member of
'vector<facetInfo,allocator<facetInfo> >' in function
cutZeros(vector<facetInfo,allocator<facetInfo>>)

Now, I read something about changing the cutZeros function to:


****** Begin Code Snippet ******
// line into main() after teh while loop
vector<facetInfo>& facinf = mySurface;
// out of main()


vector<facetInfo> cutZeros(const facetInfo& f)
{
facetInfo holder;
int goFor = mainHull.size();
int atNum = 0;
vector<facetInfo> newHull;
...
return newHull;
}

****** End Code Snippet ******


But the compiler says that 'size' is not a member of 'facetInfo' in
function 'cutZeros(const facetInfo &)'

Am I able to do this, or am I living in false hope?

Thank you again for your help, c.l.c++ - When I have a modicum of skill I
will pay your kindness back :)

Alex Livingstone
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top