473,791 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you pass a Vector to a function?

In my program I have defined a global vector with

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

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

myAve=select(mi dnum,gTmpArr,iS tart,iEnd);
.....
......
This is the function statement:
float select(int k, std:vector<floa t> &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 2738

<De****@NoSpam. com> wrote in message
news:no******** *************** *********@4ax.c om...
In my program I have defined a global vector with

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

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

myAve=select(mi dnum,gTmpArr,iS tart,iEnd);
....
.....
This is the function statement:
float select(int k, std:vector<floa t> &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<floa t> &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<floa t> &myArr, int iStart, int iEnd)
should be
float select(int k, std::vector<flo at> &myArr, int iStart, int iEnd)
Jul 22 '05 #3
"Kurt Krueckeberg" <ku***@pobox.co m> wrote:
This is the function statement:
float select(int k, std:vector<floa t> &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<floa t> &myArr, int iStart, int iEnd)
should be
float select(int k, std::vector<flo at> &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*******@pres by.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<flo at> &myArr, int iStart, int iEnd);

#endif
// end of select.h
// select.cpp
#include "select.h"

float select(const int k, std::vector<flo at> &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<flo at> &myArr, int iStart, int iEnd);

#endif
// end of select.h
// select.cpp
#include "select.h"

float select(const int k, std::vector<flo at> &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
2243
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
41
8343
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 reference?? Thanks, BB
23
6552
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: vector<string> tokenize(string s){
6
5655
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. char temp; std::vector<unsigned charmmessage; while (!done){
10
2281
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 beginner easies) way to do so? I read a bit about pointers on the web and in books but the examples do
1
2488
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
1955
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: class room { public: string description; room(string s,??) //problem here { s=description; }
0
9669
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
9515
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
10426
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...
1
10154
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.