473,407 Members | 2,359 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,407 software developers and data experts.

function parameter vector<int>*

I want to change a vector in a function. I pass a pointer of it to
the function and append an item. Then I want to print the first
item in the vector. It doesn't work. Can anyone help me?

Thanks

#include <vector>
#include <iostream>
#include "element.h"
using namespace std;
void element(vector<int>*);

int main()
{
vector<int> vecval;
element(vecval);
cout << vecval[0];
cin.ignore();
return 0;
}

//header element.h
#pragma once
#include <vector>
using namespace std;

void element(vector<int>* pval)
{
pval->push_back(5);
return;
}
Jul 23 '05 #1
6 7432
Roman Töngi wrote:
I want to change a vector in a function. I pass a pointer of it to
the function and append an item. Then I want to print the first
item in the vector. It doesn't work. Can anyone help me? This code should not have compiled at all. You're not passing a vector*
to element(), but a vector.
#include <vector>
#include <iostream>
#include "element.h"
using namespace std;
void element(vector<int>*);

int main()
{
vector<int> vecval;
element(vecval); element(&vecval); cout << vecval[0];
cin.ignore();
return 0;
}

//header element.h
#pragma once
#include <vector>
using namespace std;

void element(vector<int>* pval)
{
pval->push_back(5);
return;
}

BTW, pass the vector by reference, not by value.
#include <iostream>
#include <vector>
#include <element.h>
using namespace std;

void element(vector<int>& v)
{
v.push_back(5);
}

int main()
{
vector<int> vecval;
element(vecval);
cout << vecval[0] << endl;
return 0;
}
Jul 23 '05 #2
Roman Töngi wrote:
I want to change a vector in a function. I pass a pointer of it to
the function and append an item. Then I want to print the first
item in the vector. It doesn't work.
Always include the description of "doesn't work".
Can anyone help me?

Thanks

#include <vector>
#include <iostream>
#include "element.h"
using namespace std;
void element(vector<int>*);

int main()
{
vector<int> vecval;
element(vecval);
You're providing a vector<int> to a function that expects a pointer to
vector<int>. Either make the function parameter a reference instead of a
pointer or provide the address of vecval to the function.
cout << vecval[0];
cin.ignore();
return 0;
}

//header element.h
#pragma once
#include <vector>
using namespace std;
Never put "using namespace std;" in a header! Even in an implementation
file, you should think twice before adding it.
void element(vector<int>* pval)
{
pval->push_back(5);
return;
}


You have it kind of backwards. You put the prototype in the implementation
file and the implementation into the header file. Do it the other way
round.

Jul 23 '05 #3
I thought I could define the pointer directly in the function head.
Thank you.
Jul 23 '05 #4
Can't I put the implementation into a separate header-file?
Otherwise I have eventuelly 20 functions in the cpp-file.

Dont't use "using namespace std;" because of conflict of names?
When including the header with the "using namespace std;" directive,
its impact is restricted to header-file and is not passed to the cpp-file,
correct?

Thanks for your advice.
Jul 23 '05 #5

Roman Töngi wrote:
Can't I put the implementation into a separate header-file?
No.
Otherwise I have eventuelly 20 functions in the cpp-file.
You need to learn how to have multiple source files in your project.
How you do so is implementation-specific and you will need to find out
how to do that in your compiler documents or a newsgroup dedicated to
your platform.
Dont't use "using namespace std;" because of conflict of names?
That puts all the name from std into the gobal namespace, so yes.
When including the header with the "using namespace std;" directive,
its impact is restricted to header-file and is not passed to the cpp-file, correct?


No, not correct. Included files are effective copied and pasted into
the source file at the exact point. Any source file that includes your
header will have the using statement applied to everything after that.

What you are trying to do is the wrong way. Doing it like this will
only add to your burden.

Brian

Jul 23 '05 #6
I will engage in multiple source files.
Thanks for your help.

Roman
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andreas Krueger | last post by:
Hi! I am fed up with vector<int> iv; iv.push_back(42); iv.push_back(9); iv.push_back(11); ... and would rather use a function "fillVector": vector<int> iv = fillVector(42,9,11); like...
3
by: Erik Borgstr?m | last post by:
Hi, Yes, I have followed some of the discussion on vector<vector<int> >, but perhaps I'm just blind. I want to use a vector<vector<int> > of outer size n and inner size m. Are these correct ways...
1
by: Ingo Nolden | last post by:
Hi, I am using spirit 1.31 I have been trying the following example from the spirit docs. I tried it with int and double neither works: vector<int> v; rule<> r = list_p(int_p, ch_p(',')); ...
3
by: Rakesh Sinha | last post by:
This is about the vector template defined in standard C++ . Suppose I want to create a *huge* vector , as follows. void f1() { vector < int > data(1024); //may be not very huge, but for...
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
10
by: Piotr | last post by:
I have a class 'Statistics' which has a private attribute ' vector<int>* _x;' And in the destructor of the Statistics, I have this code to free the memory: Statistics::~Statistics() { if...
4
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
0
by: citystud | last post by:
does C# support vector< vector<int> > ?if not how can i use vector?
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
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...
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
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.