By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,428 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

problem with string class (and size function)

P: 65
the code below gives this error:
what is the problem here?

cxx: Error: str1.cpp, line 8: left operand of the "." operator must have a
class type
A.size();
---------^
cxx: Info: 1 error detected in the compilation of "str1.cpp".

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6.         string A();
  7.         A.size();
  8.  
  9.         return 0;
  10. }
Jun 7 '07 #1
Share this Question
Share on Google+
4 Replies


gpraghuram
Expert 100+
P: 1,275
HI,
I assume that u want to know the length of the string.
You want to use the function length() to get the length of the string.

Thanks
Raghuram
Jun 7 '07 #2

P: 65
length function gives the same error too
Jun 7 '07 #3

ilikepython
Expert 100+
P: 844
length function gives the same error too
The lenght() and size() function are exactly the same. Did you try actually putting some text into the string?

Edit: Also try storing the return value.
Jun 7 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
This code:

Expand|Select|Wrap|Line Numbers
  1.   string A();
  2.         A.size();
  3.  
does not create an object.

Expand|Select|Wrap|Line Numbers
  1. string A(); 
  2.  
This is a function prototype for a function named A that takes no arguments and returns a string.

Change the code to create an object:

Expand|Select|Wrap|Line Numbers
  1.   string A;
  2.         A.size();
  3.  
Jun 7 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.