CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2007
    Posts
    18

    C++ Vector question??

    I am getting these errors but I can't figure out why. I know it has something to do with the .h file but I am stumped, thanks in advance. My error messages are attached.

    //.h file

    #include <iostream>
    #include <tchar.h>
    #include <vector>

    void printarray (vector<int> *invector);



    //.cpp file

    #include "stdafx.h"
    #include <iostream>
    #include <conio.h>
    #using <mscorlib.dll>
    #include <vector>
    using namespace std;

    void printarray (vector<int> *invector)
    {
    for (int n=0; n < invector->size(); n++)
    cout << invector->at(n) << " ";
    cout << "\n";
    }

    int main ()
    {
    vector<int> FA;
    FA.push_back(5);
    FA.push_back(10);
    FA.push_back(15);

    vector<int> SA;
    SA.push_back(2);
    SA.push_back(4);
    SA.push_back(6);
    SA.push_back(8);
    SA.push_back(10);

    printarray (&FA);
    printarray (&SA);
    getch();
    return 0;
    }
    Attached Images Attached Images

  2. #2
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: C++ Vector question??

    vector does not belong to the global namespace. Under your includes, add

    Code:
    using std::vector;
    If you do'nt add that using statement (or the more general [but less desirable] using namespace std then you must qualify your use of vector by its namespace:

    Code:
    void printarray(std::vector<int>* invector);
    A side note, why are you passing a vector by pointer?
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  3. #3
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384

    Re: C++ Vector question??

    stdafx.h file not a good place put your function declarations. Besides for the current code layout, you dont even need a declaration since you are defining the function print function before it is being called/invokded and is fully visible to the compiler.

    Regards,
    Usman.

    P.S: Code Tags???
    Last edited by usman999_1; October 9th, 2007 at 09:09 AM.

  4. #4
    Join Date
    Oct 2007
    Posts
    18

    Re: C++ Vector question??

    Quote Originally Posted by Eli Gassert
    vector does not belong to the global namespace. Under your includes, add

    Code:
    using std::vector;
    If you do'nt add that using statement (or the more general [but less desirable] using namespace std then you must qualify your use of vector by its namespace:

    Code:
    void printarray(std::vector<int>* invector);
    A side note, why are you passing a vector by pointer?
    First off I am a novice with C++, if it wasn't obvious

    I don't think I am passing by pointer. I am passing a vector to the prinarray method, and then assigning invector as the pointer to the vector. Please correct me if that is not true, thanks for your reply.

  5. #5
    Join Date
    Oct 2007
    Posts
    18

    Re: C++ Vector question??

    Good point, I didn't even think of that. It ran fine when I commented out the method definition in the .h.

    Thanks.

  6. #6
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: C++ Vector question??

    Thsi code is untested...
    Code:
    void printarray (const vector<int> &invector)
    {
      for(vector<int>::const_iterator i = invector.begin(); i != invector.end(); ++i)
      {
        cout << *i << endl;
      }
    }
    You are passing it by pointer.... that's what the * is, and that's why your calls to the invector at method were using the -> opeartor.

    So when you call your method, you're calling it like
    Code:
    printarray (&FA);
    printarray (&SA);
    The & is to get the address (pointer) of the vector. You can greatly simplify your code by a) passing in by reference b) passing that reference const (const means constant meaning you're not modifying the data, only reading it) c) using iterators to iterate your data instead of indexes (which is what you were doing with the at method call).
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C++ Vector question??

    Quote Originally Posted by usman999_1
    stdafx.h file not a good place put your function declarations.
    Why not?

  8. #8
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384

    Re: C++ Vector question??

    Quote Originally Posted by Arjay
    Why not?
    Well, with all the codes/projects I've looked at in the few years that I've been working with Visual C++, none of the projects used stdafx.h for function declarations, this is more like not a rule but something like a good practice.
    You can have all the code of your project in a single cpp file , but if that makes sense is a moot point .

    Regards,
    Usman.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured