Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Vector Question

Posted on 2000-04-24
10
Medium Priority
?
314 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

885 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