why error??

This is a discussion on why error?? within the C++ Programming forums, part of the General Programming Boards category; Here is some code which I can not understand why I get an error. This is the error I get ...

  1. #1
    Unregistered
    Guest

    why error??

    Here is some code which I can not understand why I get an error.

    This is the error I get from Visual C++
    (30) : 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)

    What am I doing wrong??

    It is just reading some text from a file, putting it into a struct, then in to a vector. Now I would like to read what is in the vector.

    Would appreciate any pointers.
    Thanks



    #include <iostream.h>
    #include <stdlib.h>
    #include <vector>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <cctype>
    #include <cstdlib>

    using namespace std;

    struct course
    {
    string name;
    string num;
    int credits;
    vector <string> preq;
    };

    vector <course> read_catalog(vector<course> &catalog);

    int main()
    {
    vector <course> catalog;

    read_catalog(catalog);
    //system("PAUSE");

    for(int i=0; i<catalog.size(); i++)
    cout << catalog[i].name << endl;

    return 0;

    }

    vector <course> read_catalog(vector <course> &catalog)
    {
    string line, part;
    int counter;
    course c;

    ifstream fin("catalog.txt");

    if(fin.fail())
    {
    cout<<"Failure to open file";
    return catalog;
    }



    while(!fin.eof())
    {
    getline(fin,line); //taking in a whole line from file
    stringstream sin(line); //converting string line to a stream
    counter=1;
    while(true)
    {
    getline(sin, part, ','); //taking a line until a comma is found
    if(part=="")
    break;
    if(counter==1)
    c.name = part;
    if(counter==2)
    c.num = part;
    if(counter==3)
    {
    const char *st = part.c_str(); //string part is convert to cstring
    c.credits = atoi(st); //st is converted to an integer and put in struct
    }
    if(counter>3)
    (c.preq).push_back(part);
    counter++;
    }
    }

    return catalog;
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    you smarter guys can correct me if im wrong, but if you use a struct or class to define a data type, you cant use the << or >> operators unless you overload them.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Try getting rid of the .h at the end of iostream. (and making it <cstdlib> probably wouldn't hurt either).
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Unregistered
    Guest
    okay made a few changed, but still stuck, looks like in inner loop

    i am trying to convert a string to a stream, then use getline to extract part of by string to the comma, but it doesn't seem to be working

    any ideas

    catalog.txt has lines like:

    "Intro to C, CS101, 3, CS100"

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <vector>
    #include <fstream>
    #include <string>
    #include "sstream.h"
    
    using namespace std;
    
    struct course
      {
      string name;
      string num;
      int credits;
      vector <string> preq;
      };
    
    vector <course> read_catalog(vector<course>&catalog);
    
    int main()
    {
      vector <course> catalog;
      read_catalog(catalog);
          system("PAUSE");
          return 0;
    }
    
    vector <course> read_catalog(vector <course> &catalog)
     {
     string line, part;
     int counter;
     course c;
    
      ifstream fin("catalog.txt");
    
      if(fin.fail())
         {
         cout<<"Failure to open file";
         return catalog;
         }
    
    
    
      while(!fin.eof())
           {
            getline(fin,line);       //taking in a whole line from file
            // is this right???????????????????????????????????
            stringstream sin(line);  //converting string line to a stream
            counter=1;
            while(true)
              {
               getline(sin, part, ',');  //taking a line until a comma is found
               if(part.size()==0)
                 break;
               if(counter==1)
                 c.name = part;
               if(counter==2)
                 c.num = part;
               if(counter==3)
                 {
                 const char *st = part.c_str();   //string part is convert to cstring
                 c.credits = atoi(st);     //st is converted to an integer and put in struct
                 }
               counter++;
               }
               catalog.push_back(c);
    
               /* if(counter>3)
                 (c.preq).push_back(part);      // ????? is this right??????     */
            }
            cout << line << endl;
      return catalog;
     }

  5. #5
    Unregistered
    Guest
    this i got to compile in bloodshed, by the way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21