By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,385 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

function parameter vector<int>*

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
I thought I could define the pointer directly in the function head.
Thank you.
Jul 23 '05 #4

P: n/a
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

P: n/a

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

P: n/a
I will engage in multiple source files.
Thanks for your help.

Roman
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.