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;
} 6 7470
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;
}
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.
I thought I could define the pointer directly in the function head.
Thank you.
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.
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
I will engage in multiple source files.
Thanks for your help.
Roman This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 in Mathematica(tm): someList={42,9,11};
|
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 to achieve this?
1.
vector<vector<int> > v(n);
for(int i=0;i<n;i++){
v = vector<int>(m);
|
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(','));
but it gives the weird messages:
|
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
//demonstration purposes
//Process 'data'
|
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 the vector with the
values I need (I know these values before hand).
- What's the best way to initialize the vector<vector<int> >? Can I
initilize it by enumerating its values?
- If I do: v = new vector<vector<int> >(3) for example, is it
| |
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 (_x) {
|
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 vector
|
by: citystud |
last post by:
does C# support vector< vector<int> > ?if not how can i use vector?
|
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++ Primer - 4/e
*
* exercise 9.20
* STATEMENT:
* Write a program to to compare whether a vector<intcontains the
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |