C++ Help

Xedus

New member
could you please tell me why this won't compile!!
i assume strings in c++ work in the same way it does in java but i get 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)

here is the code

Code:
#include <iostream.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
 
using namespace std;
struct Book{
 string name;
 int pages;
 double price;
};
void main()
{
 Book b;
 b.name = "bbbb";
 b.pages = 50;
 b.price = 19.95;
 
 cout<<b.name;
 cout<<b.pages;
 cout<<b.price;
 
 
}

how do i get to print a string!?
 
Xedus said:
are there any workarounds?
just tested with VC++6sp6, works fine.
Code:
#include <iostream>
#include <string>

using std::cout;
using std::string;

struct Book{
 string name;
 int pages;
 double price;
};
int main()
{
 Book b;
 b.name = "bbbb";
 b.pages = 50;
 b.price = 19.95;
 
 cout<<b.name;
 cout<<b.pages;
 cout<<b.price;
 return 0;
 
}
 
Last edited:
thank you now i have it done!

i had to include <iostream> and "using std::cout" to make it work although i did not need to inorder to print anything other than a string. any body can explain this to me i'll be grateful
 
Xedus said:
thank you now i have it done!

i had to include <iostream> and "using std::cout" to make it work although i did not need to inorder to print anything other than a string. any body can explain this to me i'll be grateful
there's a difference in the definition of iostream and iostream.h I believe.
 
Back
Top