Archived

This topic is now archived and is closed to further replies.

need help with basic_string

This topic is 6035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings All, I am using trying to use basic_string although I keep getting errors. In one book I am reading it uses the old iostream library and it is as follow:
int main() { std::string str1(); std::string str2("This is a string"); std::string str3("Today is sunny"); cout << "str1 " << str1 << endl; cout << "str2 " << str2 << endl; cout << "str3 " << str3 << endl; return 0; } ===================================================== I rewrote it for the old iostream library. #include #include using namespace std; int main() { string str1(); string str2("This is a string"); string str3("Today is sunny"); cout << "str1 " << str1 << endl; cout << "str2 " << str2 << endl; cout << "str3 " << str3 << endl; return 0; } I get the error: no operand defined that takes a right-hand operand..... Any help is appreciated. Thanks. ----------------------------- "There are ones that say they can and there are those who actually do." "...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites
quote:
Original post by cMADsc

#include <iostream>
#include <string>

using namespace std;

int main()
{
string str1();
string str2("This is a string");
string str3("Today is sunny");

cout << "str1 " << str1 << endl;
cout << "str2 " << str2 << endl;
cout << "str3 " << str3 << endl;

return 0;
}
I get the error: no operand defined that takes a right-hand operand.....


It''s a bad idea to mix old (.h) and new (no .h) style headers, because it will often lead to problems. Best is to stick with the new style headers, because they''re the only ones allowed according to the C++ standard.

However, that is not the problem in your code. This is the problem:

string str1();

This doesn''t define a variable str1 of type string. Instead, it declares a function called str1, with no arguments, that returns a string. if you change it to

string str1;

it will do what you want (create an empty string).

Another tip: if you post error messages, be sure to post the complete message. In this case, you left out the most important part of the error.

HTH



Some useful C++ links:
Free multiplatform ANSI C++ Standard Library implementation
Visual C++ STL fixes
Visual C++ 6.0 noncompliance issues
C++ FAQ Lite

Share this post


Link to post
Share on other sites