problem with string class (and size function)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stmfc
    New Member
    • May 2007
    • 65

    problem with string class (and size function)

    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".

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
            string A();
            A.size();
    
            return 0;
    }
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    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

    Comment

    • stmfc
      New Member
      • May 2007
      • 65

      #3
      length function gives the same error too

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by stmfc
        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.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          This code:

          [code=cpp]
          string A();
          A.size();
          [/code]

          does not create an object.

          [code=cpp]
          string A();
          [/code]

          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:

          [code=cpp]
          string A;
          A.size();
          [/code]

          Comment

          Working...