468,765 Members | 1,368 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

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 7137
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Andreas Krueger | last post: by
3 posts views Thread by Erik Borgstr?m | last post: by
1 post views Thread by Ingo Nolden | last post: by
3 posts views Thread by Rakesh Sinha | last post: by
4 posts views Thread by arnuld | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.