function parameter vector<int>*

R

Roman Töngi

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;
}
 
R

red floyd

Roman said:
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;
}
 
R

Rolf Magnus

Roman said:
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
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.
 
R

Roman Töngi

I thought I could define the pointer directly in the function head.
Thank you.
 
R

Roman Töngi

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.
 
D

Default User

Roman said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top