472,793 Members | 2,275 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,793 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 7403
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++...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.