function parameter vector<int>*

Discussion in 'C++' started by Roman Töngi, Apr 5, 2005.

  1. Roman Töngi

    Roman Töngi Guest

    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;
    }
    Roman Töngi, Apr 5, 2005
    #1
    1. Advertising

  2. Roman Töngi

    red floyd Guest

    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;
    }
    red floyd, Apr 5, 2005
    #2
    1. Advertising

  3. Roman Töngi

    Rolf Magnus Guest

    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.
    Rolf Magnus, Apr 5, 2005
    #3
  4. Roman Töngi

    Roman Töngi Guest

    I thought I could define the pointer directly in the function head.
    Thank you.
    Roman Töngi, Apr 5, 2005
    #4
  5. Roman Töngi

    Roman Töngi Guest

    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, Apr 5, 2005
    #5
  6. Roman Töngi

    Default User Guest

    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
    Default User, Apr 5, 2005
    #6
  7. Roman Töngi

    Roman Töngi Guest

    I will engage in multiple source files.
    Thanks for your help.

    Roman
    Roman Töngi, Apr 6, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,740
  2. Schnoffos
    Replies:
    2
    Views:
    1,199
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,617
    Old Wolf
    Jan 20, 2004
  4. Replies:
    8
    Views:
    1,898
    Csaba
    Feb 18, 2006
  5. jdm
    Replies:
    1
    Views:
    653
    Victor Bazarov
    May 18, 2010
Loading...

Share This Page