473,785 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a vector<customSt ructure> to functions outside main()

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<facetInf o> 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.getl ine(buffer, 128, ' ').eof())
{
... // fills mySurface with 'mySurface.push _back(holdMe);' s
}
}

vector<facetInf o> cutZeros(vector <facetInfo> mainHull)
{
facetInfo holder;
int goFor = mainHull.size() ;
int atNum = 0;
vector<facetInf o> 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_ba ck(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<facetIn fo,allocator<fa cetInfo> >' in function
cutZeros(vector <facetInfo,allo cator<facetInfo >>)

Now, I read something about changing the cutZeros function to:
****** Begin Code Snippet ******
// line into main() after teh while loop
vector<facetInf o>& facinf = mySurface;
// out of main()
vector<facetInf o> cutZeros(const facetInfo& f)
{
facetInfo holder;
int goFor = mainHull.size() ;
int atNum = 0;
vector<facetInf o> 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

--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
Jul 19 '05 #1
5 3193

"{AGUT2} {H}-IWIK" <al************ ********@ambtin ternet.com> wrote in message
holder.v1x = mainHull.v1x;


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

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.
Jul 19 '05 #2
> mainHull isn't a facetinfo, it's a vector<facitinf o>

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

I apologise for the silly question.

Alex.

--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...
Jul 19 '05 #3
{AGUT2} {H}-IWIK wrote in news:op******** ******@mercury. nildram.net:
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<facetInf o> cutZeros(vector <facetInfo> mainHull)
{
Note in this case:

vector<facetInf o> 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<facetInf o> 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_ba ck(holder);
} // end3 if
} // end2 if
} // end1 if
atNum++;
} // endwhil
return newHull;
}

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


[snip]

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4

"{AGUT2} {H}-IWIK" <al************ ********@ambtin ternet.com> wrote in
message news:op******** ******@mercury. nildram.net...
mainHull isn't a facetinfo, it's a vector<facitinf o>


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
Jul 19 '05 #5
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" <al************ ********@ambtin ternet.com> wrote in
message news:op******** ******@mercury. nildram.net...
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<facetInf o> 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.getl ine(buffer, 128, ' ').eof())
{
... // fills mySurface with 'mySurface.push _back(holdMe);' s
}
}

vector<facetInf o> cutZeros(vector <facetInfo> mainHull)
{
facetInfo holder;
int goFor = mainHull.size() ;
int atNum = 0;
vector<facetInf o> 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_ba ck(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<facetIn fo,allocator<fa cetInfo> >' in function
cutZeros(vector <facetInfo,allo cator<facetInfo >>)

Now, I read something about changing the cutZeros function to:
****** Begin Code Snippet ******
// line into main() after teh while loop
vector<facetInf o>& facinf = mySurface;
// out of main()
vector<facetInf o> cutZeros(const facetInfo& f)
{
facetInfo holder;
int goFor = mainHull.size() ;
int atNum = 0;
vector<facetInf o> 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

--
Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual...

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1242
by: ILLOGIC | last post by:
Hello, i am beginner in c++. I hope tobe sufficiently clear and that someone could help me on this topic. For example i have template function <typename T> T sin_func(T & x){return sin(x);} could it be possible that the paramenter of the function is another template function? An example would be very helpful
7
2497
by: Steven T. Hatton | last post by:
I am trying to convert some basic OpenGL code to an OO form. This is the C version of the program: http://www.opengl.org/resources/code/basics/redbook/double.c You can see what my current effort at converting that code to an OO form looks like in the code listed below. The problem I'm running into is that the OpenGL functions that take the names of other functions as arguments don't like the way I'm passing the member functions of my...
1
1563
by: Cplusplus | last post by:
as C++ defaults to external linkage for non-consts and non-static(right?): Qn) why is it then : a object declared (*without* static keyword)outside main() and outside all functions , automatically considered a static object ?. doesn't static mean its internal linkage? accding me if u define a object like this then A should be external linkage. Obj A('a'); // Global int main()
3
2284
by: Steven Taylor | last post by:
Hope someone can assist. I'm trying to answer a book question. I'm going around in circles in relation to 'pointer-to-char'. Object : A short program is to be created, which involves a structore and one member element is a char array. A function is to be created that passes the structure as a reference, along with the 'pointer-to-char'. The function is to assign the passed-in 'pointer-to-char' to the referenced (passed-in)...
10
1531
by: Merrill & Michele | last post by:
The following progs differ only in the location of the prototype (<--adjective) definition with respect to the main call. Both build and behave for me. Is there a difference that amounts to something once the subject matters gets stickier? MPJ #include <stdio.h> int main(void){ int i = 5; void increment(int *); increment(&i); printf("i = %d\n",i);
3
1922
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford www.IntraRELY.com
11
4432
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI components. The application is built around a "World" object that contains a "GUI" object that is a C++ wrapper around GLUT. What I'd like to do is pass a pointer to a member function in the World class to function in the GUI
2
1968
satterfieldben
by: satterfieldben | last post by:
I have a newbie question about passing variables between functions. I am wanting to use a drop down box to select a value. Then base on which was selected, it would create a variable and I would call that variable in another Java script. Sample script <SCRIPT LANGUAGE="JavaScript"> function GetSelectedItem() { len = document.formname.selectname.length i = 0 chosen = "none"
2
1405
by: nalinibh | last post by:
hello can anybody explain to me how can i put a timer in my coding but outside main. i have tried clock and time already thanks nalinibh
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.