Solved

Vector Question

Posted on 2000-04-24
10
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

635 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