Solved

Vector Question

Posted on 2000-04-24
10
306 Views
Last Modified: 2013-12-14
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???
0
Comment
Question by:junkyboy
  • 6
  • 3
10 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2743601
>> whenever I try to cout the vector, it gives
>> me a compilation error saying:
>> binary <<: no operator defined.......

The error message is correct and should be pretty clear.  The vecotr class does not have an overloaded operator <<  so you cannot ouput a vector with <<.  You have two options.  You could write your own global operator << for ouputting vectors or you could just use a loop to ouput the information in the vector directly.  (note these aren't really distinct options, the global operator << you write would have to use a loop to ouput the information.)

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2743615
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.
0
 

Author Comment

by:junkyboy
ID: 2743791
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)
0
 
LVL 22

Expert Comment

by:nietod
ID: 2743836
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2743840
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:Wyn
ID: 2743841
->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...
0
 
LVL 22

Expert Comment

by:nietod
ID: 2743866
>> 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.)
0
 

Author Comment

by:junkyboy
ID: 2743881
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?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2743960
You still have the same problem.  You are using <iostream.h>  It should be <iostream> these days.
0
 

Author Comment

by:junkyboy
ID: 2743967
Thanks everyone!  It works perfectly!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now