Link to home
Start Free TrialLog in
Avatar of junkyboy
junkyboy

asked on

Vector Question

I have the following code:

//-------------------------------------

vector<string> FileList;

BOOL  Done;
HANDLE  FndHnd = NULL;
WIN32_FIND_DATA FindDat;
FndHnd = FindFirstFile("*.*", &FindDat);
Done = (FndHnd == INVALID_HANDLE_VALUE);
while (!Done)
{
   string FileName = FindDat.cFileName;
   FileList.push_back(FileName);
   Done = !FindNextFile(FndHnd, &FindDat);
   cout << FileList;
}
if (FndHnd)
   FindClose(FndHnd);

//-------------------------------------

I've never used vectors before, and whenever I try to cout the vector, it gives me a compilation error saying: binary <<: no operator defined.......
and it doesnt run.  I'm using MSVC++6.0 on a Win2000Pro.  Any suggestions???
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

To use a loop you would do something like

const int ItmCnt = FileList.size();

for (int i = 0; i < ItmCnt; ++i)
    cout << FileList[i];

or you could use iterators, like

vector<string>::iterator Itr = FileList.begin();

while (itr != FileList.end())
{
    cout << *Itr;
   ++Itr;
}

Let me know if you have any questions.
Avatar of junkyboy

ASKER

This is what I have so far:

//------------------------------------

      vector<string> FileList;

      BOOL  Done;
      HANDLE  FndHnd = NULL;
      WIN32_FIND_DATA FindDat;
      FndHnd = FindFirstFile("*.*", &FindDat);
      Done = (FndHnd == INVALID_HANDLE_VALUE);
      while (!Done)
      {
         string FileName = FindDat.cFileName;
         FileList.push_back(FileName);
         Done = !FindNextFile(FndHnd, &FindDat);
      }
      if (FndHnd)
         FindClose(FndHnd);
      const int ItmCnt = FileList.size();
      for (int i = 0; i < ItmCnt; ++i)
          cout << FileList[i];
      cout << endl;
      getch();

//------------------------------------

It still doesn't compile.  It still gives me the following error:

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_
traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
You should not be getting that error.

Do you include <string.h> or <string>?   You might get that error with <string.h>.  That is an old, non-standard file and should not be used anymore.  Use <string> instead.
note if you use <string> all the names will be defined in the "std" namespace, so you need to add a using statement like

using namespace std;

or you need to place a "std::" before the "string" type name.
->The vecotr class does not have an overloaded operator << 
=========================
nietod , I think this sentence should be : the ostream doesnt overload << for vector class...
or no global operator << defined for ostream and vector...

I just think << is subject to the one before it ,otherwise a gloabal redefined operator...  
Correct me if i'm wrong...
Regards
Eirnava...
>> nietod , I think this sentence should be
true--sort of.   Usually ouput operator << is handled as a non-member function.  Since the function is not included in the stream classes (except for the built-in types) all other classes that need to support it write it as a non-member function.  However, this non-member function is in some ways considered to be part of the class that is being outputted, like it can be considered to be part of the interface of that class.   When I said that vector has no overloaded << operator, I really mean that there was no non-member overloaded << operator written that takes a vector.  (Although one could be written.)
This is the whole thing:

#include <vector>
#include <iostream.h>
#include <conio.h>
#include <string>
#include <windows.h>

using namespace std;

void main()
{
      vector<string> FileList;

      BOOL  Done;
      HANDLE  FndHnd = NULL;
      WIN32_FIND_DATA FindDat;
      FndHnd = FindFirstFile("*.*", &FindDat);
      Done = (FndHnd == INVALID_HANDLE_VALUE);
      while (!Done)
      {
         string FileName = FindDat.cFileName;
         FileList.push_back(FileName);
         Done = !FindNextFile(FndHnd, &FindDat);
      }
      if (FndHnd)
         FindClose(FndHnd);
      const int ItmCnt = FileList.size();
      for (int i = 0; i < ItmCnt; ++i)
          cout << FileList[i];
      cout << endl;
      getch();
}

It still gives me the same error, even after changing from string.h to string.  Any suggestions?
You still have the same problem.  You are using <iostream.h>  It should be <iostream> these days.
Thanks everyone!  It works perfectly!