By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

How do you pass a Vector to a function?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

<De****@NoSpam.com> wrote in message
news:no********************************@4ax.com...
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
Jul 22 '05 #2

P: n/a
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)
Jul 22 '05 #3

P: n/a
"Kurt Krueckeberg" <ku***@pobox.com> wrote:
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)

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

Jul 22 '05 #4

P: n/a
In article <8g********************************@4ax.com>,
<De****@NoSpam.com> wrote:
[...]
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.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #5

P: n/a
>
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
Jul 22 '05 #6

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote:

<snip>

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

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.