How do you pass a Vector to a function?

D

Dennis

In my program I have defined a global vector with

//=======prototypes section ====================
float select(const int k, std::vector<float> &myArr, int iStart, int iEnd);
.....
//====Global Vectors ==================================
std::vector<float> gTmpArr(8000,0);

In Main, I would like to pass this vector to a function such as:

myAve=select(midnum,gTmpArr,iStart,iEnd);
.....
......
This is the function statement:
float select(int k, std:vector<float> &myArr, int iStart, int iEnd)

However I get the following error:

error C2061: syntax error : identifier 'std'
error C2065: 'iEnd' : undeclared identifier
error C2065: 'iStart' : undeclared identifier
error C2065: 'myArr' : undeclared identifier

What is the correct way to define the prototype and pass the vector?

Thanks for any help.

Dennis
 
J

John Harrison

In my program I have defined a global vector with

//=======prototypes section ====================
float select(const int k, std::vector<float> &myArr, int iStart, int
iEnd);
....
//====Global Vectors ==================================
std::vector<float> gTmpArr(8000,0);

In Main, I would like to pass this vector to a function such as:

myAve=select(midnum,gTmpArr,iStart,iEnd);
....
.....
This is the function statement:
float select(int k, std:vector<float> &myArr, int iStart, int iEnd)

However I get the following error:

error C2061: syntax error : identifier 'std'
error C2065: 'iEnd' : undeclared identifier
error C2065: 'iStart' : undeclared identifier
error C2065: 'myArr' : undeclared identifier

What is the correct way to define the prototype and pass the vector?

Exactly like you have shown. Probably your mistake is not including the
header file <vector>, but as usual it is impossible to tell because you
didn't post all of your code. Whatever your mistake is, it is in the code
you didn't post, not the code you did.

john
 
K

Kurt Krueckeberg

This is the function statement:
float select(int k, std:vector<float> &myArr, int iStart, int iEnd)

However I get the following error:

error C2061: syntax error : identifier 'std'
error C2065: 'iEnd' : undeclared identifier
error C2065: 'iStart' : undeclared identifier
error C2065: 'myArr' : undeclared identifier

What is the correct way to define the prototype and pass the vector?

You forgot a colon.

float select(int k, std:vector<float> &myArr, int iStart, int iEnd)
should be
float select(int k, std::vector<float> &myArr, int iStart, int iEnd)
 
D

Dennis

Kurt Krueckeberg said:
You forgot a colon.

float select(int k, std:vector<float> &myArr, int iStart, int iEnd)
should be
float select(int k, std::vector<float> &myArr, int iStart, int iEnd)
Thank you John and Kurt.

I had two errors in my code. The simple one "::" that Kurt pointed out above
and one that I didn't know about.

My code consists of too many lines to post so I only posted the trouble part of
the code.

The Main function is in a separate module with an #Include <vector> before the
function Main. My function "Select" is in another module. Both source file
modules are compiled by VC++6 . What I didn't know was that I had to also put a
#Include <vector> in the Select Function module before the function name. Thus
I needed a #Include <vector> in both modules in order for this code to compile.

Why is that? I thought defining #Include <vector> at the top of your code
before the start of your functions was enough and would be available to all
functions below? The Main module is above the Select function module in the
source file definitions.

Dennis
 
J

Jon Bell

I needed a #Include <vector> in both modules in order for this code to compile.

Why is that? I thought defining #Include <vector> at the top of your code
before the start of your functions was enough and would be available to all
functions below?

Not if the source code is in separate files. As far as the compiler (as
opposed to the linker) is concerned, each file is completely independent.
All identifiers used in any particular file must be declared in that file,
either directly via declarations in that file, or indirectly via
declarations in headers that are #include'd in that file.

VC++ may display the contents of the source code files one after the
other as if they formed a single unit (I don't use VC++ myself so I'm just
guessing), but that would be just a display device of the GUI.
 
J

John Harrison

The Main function is in a separate module with an #Include <vector>
before the
function Main. My function "Select" is in another module. Both source
file
modules are compiled by VC++6 . What I didn't know was that I had to also
put a
#Include <vector> in the Select Function module before the function name.
Thus
I needed a #Include <vector> in both modules in order for this code to
compile.

Right.

Why is that?

Because the two modules are compiled seperately.
I thought defining #Include <vector> at the top of your code
before the start of your functions was enough and would be available to
all
functions below?

Modules are compiled seperately, what one module contains is completely
unknown to any other module.
The Main module is above the Select function module in the
source file definitions.

I don't understand what you mean by that. If you are refering to the order
in which your source files are listed in VC++6 that is completely
irrelevant. Modules are still compiled seperately.

Here's how you should organise your code. When you have something in common
between two modules you should us a header file and include that header file
in both modules. In this case its the select function. So...

// select.h
#ifndef SELECT_H
#define SELECT_H

#include <vector>

float select(const int k, std::vector<float> &myArr, int iStart, int iEnd);

#endif
// end of select.h


// select.cpp
#include "select.h"

float select(const int k, std::vector<float> &myArr, int iStart, int iEnd)
{
...
}
// end of select.cpp


// main.cpp
#include "select.h"

int main()
{
...
select(a, b, c, d);
...
}
// end of main.cpp

Note especially how the select.h file includes the <vector> header. And also
that both main.cpp and select.cpp include select.h

Organising your source code properly seems to be one of those things that
books on C++ don't teach, but the separate compilation model means it is
vital to have a good understanding of this. It's easy once you've been shown
how, however.

john
 
D

Dennis

Here's how you should organise your code. When you have something in common
between two modules you should us a header file and include that header file
in both modules. In this case its the select function. So...

// select.h
#ifndef SELECT_H
#define SELECT_H

#include <vector>

float select(const int k, std::vector<float> &myArr, int iStart, int iEnd);

#endif
// end of select.h


// select.cpp
#include "select.h"

float select(const int k, std::vector<float> &myArr, int iStart, int iEnd)
{
...
}
// end of select.cpp


// main.cpp
#include "select.h"

int main()
{
...
select(a, b, c, d);
...
}
// end of main.cpp

Note especially how the select.h file includes the <vector> header. And also
that both main.cpp and select.cpp include select.h

Organising your source code properly seems to be one of those things that
books on C++ don't teach, but the separate compilation model means it is
vital to have a good understanding of this. It's easy once you've been shown
how, however.

john
Thank you John for the detailed explanation. It's one of those lessons that you
never see in books. I appreciate the time you spent in explaining it to me.
Thanks again.

Thanks also to Jon Bell in the reply above.

Dennis
 

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

Forum statistics

Threads
474,183
Messages
2,570,970
Members
47,527
Latest member
RoxanneTos

Latest Threads

Top