Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2337
  • Last Modified:

Beginner: Error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'const class std::

Everytime i try to run the code below, i keep on getting this error message:
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)
whats wrong with the code? i cant seem to figure it out.

heres the code:
---------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <ctime>

using std::cout;
using std::endl;
using std::setw;
using std::string;

const int NUM_GRADES = 10;
const int NUM_STUDENTS = 3;

int findHighest( int * );
int findLowest( int * );
void printDatabase( const int [][NUM_GRADES], const string [NUM_STUDENTS] );

int main()
{
   int student1[ NUM_GRADES ] = { 0 };
   int student2[ NUM_GRADES ] = { 76, 89, 81, 42, 66, 93, 104, 91, 71, 85};
   int student3[ NUM_GRADES ] = { 65, 69, 91, 89, 82, 93, 72, 76, 79, 99 };
   int database[ NUM_STUDENTS ][ NUM_GRADES ];
   string names[ NUM_STUDENTS ] = {"Bob", "John", "Joe"};
   
   int i=0;

   srand( time( 0 ) );
   
   // initialize student1
   for ( i = 0; i < NUM_GRADES; i++ )
      student1[ NUM_GRADES ] = rand() % 50 + 50;

   // initialize database
   for ( i = 1; i < NUM_GRADES; i++ ) {
      database[ 0 ][ i ] = student1[ i ];
      database[ 1 ][ i ] = student2[ i ];
      database[ 2 ][ i ] = student3[ i ];
   }

   printDatabase( database, names );

   for ( i=0; i < NUM_STUDENTS; i++ )
   {
      cout << names[i] << "'s highest grade is: " << findHighest( student1 ) << endl
           << names[i] << "'s lowest grade is: " << findLowest( database[ i ] ) << endl;
   }

   return 0;
}

int findHighest( int a[] )
{
   int highest = a[ 0 ];
   
   for ( int i = 1; i <= NUM_GRADES; i++ )
      if ( a[ i ] > highest )
         highest = a[ i ];
   
   return highest;
}

int findLowest( int a[] )
{
   int lowest = a[ 0 ];
   
   for ( int i = 1; i < NUM_GRADES; i++ )
      if ( a[ i ] < lowest )
         lowest = a[i];
   
   return lowest;
}

void printDatabase( const int a[][ NUM_GRADES ], const string names[NUM_STUDENTS] )
{
   cout << "Here is the grade database\n\n"
        << setw( 10 ) << "Name";
   
   for ( int n = 1; n <= NUM_GRADES; n++ )
      cout << setw( 4 ) << n;

   cout << endl;
   
   for ( int i = 0; i < NUM_STUDENTS; i++ ) {
      cout << setw( 10 ) << names[i];
     
      for ( int j = 0; j < NUM_GRADES; j++ )
         cout << setw( 4 ) << a[ i, j ];

      cout << endl;
   }
   cout << endl;
}
0
jhson114
Asked:
jhson114
  • 2
  • 2
  • 2
  • +2
2 Solutions
 
KalvynCommented:
use names[i].c_str()
0
 
mnashadkaCommented:
You forgot #include <string>.  That's where the stream insertion operator lives.  Good luck.
0
 
jhson114Author Commented:
thanks alot guys! it works!! :)
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.

 
bcladdCommented:
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to mnashadka

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0
 
KalvynCommented:
I believe it was my comment that ansered his question.  Points should be awarded to Kalvyn.
0
 
mnashadkaCommented:
Kalvyn, you're response helped show jhson114 how to convert his/her string into a const char *, not how to insert a string into a stream.  For that, you just need to #include <string> which is where the stream insertion operator for a basic_string lives, and then you can use the stream insertion operator on a string (without calling c_str()).  Maybe we should split them.  Of course, ideally jhson114 would award the points.
0
 
ewweCommented:
I still have the same problem. Please help. Here is my code:

#include <iostream.h>
#include <string>                              

using namespace std;

int main (void){
    string strOne, strTwo, myStr;
    strOne = "one";
    strTwo = " and two";
    myStr = strOne + strTwo;
    cout << myStr << endl;               //this line generates the error

    system("PAUSE");
    return 0;
}

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)

I am using Visual Studio 6 IDE.

Thanks
0
 
ewweCommented:
My problem gone. I removed .h from #include<iostream>. The problem is gone. Thanks all the same.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now