Solved

Vector Question

Posted on 2000-04-24
10
305 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

15 Experts available now in Live!

Get 1:1 Help Now