Connecting Tech Pros Worldwide Forums | Help | Site Map

How do you pass a Vector to a function?

Dennis@NoSpam.com
Guest
 
Posts: n/a
#1: Jul 22 '05
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



John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: How do you pass a Vector to a function?



<Dennis@NoSpam.com> wrote in message
news:no8ap0htuf8227roc38m3sjojrpfepqh2s@4ax.com...[color=blue]
> 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?[/color]

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


Kurt Krueckeberg
Guest
 
Posts: n/a
#3: Jul 22 '05

re: How do you pass a Vector to a function?


[color=blue]
> 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?[/color]

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)


Dennis@NoSpam.com
Guest
 
Posts: n/a
#4: Jul 22 '05

re: How do you pass a Vector to a function?


"Kurt Krueckeberg" <kurtk@pobox.com> wrote:
[color=blue]
>[color=green]
>> 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?[/color]
>
>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)
>[/color]
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

Jon Bell
Guest
 
Posts: n/a
#5: Jul 22 '05

re: How do you pass a Vector to a function?


In article <8geap0l48i7gpr3qlomddd09hhg4ujvp6l@4ax.com>,
<Dennis@NoSpam.com> wrote:
[...][color=blue]
>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?[/color]

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 <jtbellm4h@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
John Harrison
Guest
 
Posts: n/a
#6: Jul 22 '05

re: How do you pass a Vector to a function?


>[color=blue]
> 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.
>[/color]

Right.
[color=blue]
> Why is that?[/color]

Because the two modules are compiled seperately.
[color=blue]
> 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?[/color]

Modules are compiled seperately, what one module contains is completely
unknown to any other module.
[color=blue]
> The Main module is above the Select function module in the
> source file definitions.[/color]

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


Dennis@NoSpam.com
Guest
 
Posts: n/a
#7: Jul 22 '05

re: How do you pass a Vector to a function?


"John Harrison" <john_andronicus@hotmail.com> wrote:

<snip>[color=blue]
>
>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
>[/color]
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



Closed Thread