473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How do you pass a Vector to a function?

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
6 2719

<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
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
"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
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
>
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
10
by: Bernhard Reinhardt | last post by:
Hi, I read data from a file into a 4 dimensional array. The data has a size of 5MB (could later be up to 500MB). I want to pass the matrix to subroutines. What is the cleverst (and for a...
1
by: Lambda | last post by:
I defined a class: class inverted_index { private: std::map<std::string, std::vector<size_t index; public: std::vector<size_tintersect(const std::vector<std::string>&); };
6
by: Poke386 | last post by:
I'm in the process of making an text based-rpg in C++. Its just a little project so I can learn some object-oriented programming, nothing serious. My problem is that I've created a class like so: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.