stupid syntax errors

Discussion in 'C++' started by Puppet_Sock, Feb 13, 2007.

  1. Puppet_Sock

    Puppet_Sock Guest

    So, I'm madly coding away, and my fingers stutter, and I produce this.
    (mfirstToken is a std::string object.)

    if(m_firstToken.c_str()[0] == 'M' || m_firstToken.c_str().c_str()[0]
    == 'T')
    {
    // ... other code here
    }

    And of course my compiler barfs over it. But what it tells me is
    ..c_str requires an object. Had to peer at that for like 20 minutes
    to spot the stupid mistake. I should not drink and code.
    Socks
     
    Puppet_Sock, Feb 13, 2007
    #1
    1. Advertising

  2. Puppet_Sock

    red floyd Guest

    Puppet_Sock wrote:
    > So, I'm madly coding away, and my fingers stutter, and I produce this.
    > (mfirstToken is a std::string object.)
    >
    > if(m_firstToken.c_str()[0] == 'M' || m_firstToken.c_str().c_str()[0]
    > == 'T')
    > {
    > // ... other code here
    > }
    >
    > And of course my compiler barfs over it. But what it tells me is
    > .c_str requires an object. Had to peer at that for like 20 minutes
    > to spot the stupid mistake. I should not drink and code.
    > Socks
    >


    I'm pretty sure that everyone on this list has a stupid syntax
    error/logic error story.

    Mine is from the first piece of code I wrote as a professional (C only,
    C++ didn't exist back then).

    int init()
    {
    int i;
    for (i = 0 ; i < LIMIT ; i++);
    {
    /* initialize array here */
    }
    }

    Took me forever to figure out why nothing was getting initialized, and
    some memory was getting corrupted.
     
    red floyd, Feb 13, 2007
    #2
    1. Advertising

  3. Puppet_Sock wrote:
    > So, I'm madly coding away, and my fingers stutter, and I produce this.
    > (mfirstToken is a std::string object.)
    >
    > if(m_firstToken.c_str()[0] == 'M' || m_firstToken.c_str().c_str()[0]
    > == 'T')
    > {
    > // ... other code here
    > }
    >
    > And of course my compiler barfs over it. But what it tells me is
    > .c_str requires an object. Had to peer at that for like 20 minutes
    > to spot the stupid mistake. I should not drink and code.
    > Socks
    >


    Drinking and posting also has it's hazards too.

    if (m_firstToken[0] == 'M' || m_firstToken[0] == 'T')
    {
    // ... other code here
    }

    I assume m_firstToken is a std::string.

    Usually the best thing to do when parsing a string is to place the value
    into a temporary. i.e.

    const char tmp = m_firstToken[0];

    if (tmp == 'M' || tmp == 'T')
    {
    // ... other code here
    }
     
    Gianni Mariani, Feb 13, 2007
    #3
  4. On 2007-02-13 21:06, Gianni Mariani wrote:
    > Puppet_Sock wrote:
    >> So, I'm madly coding away, and my fingers stutter, and I produce this.
    >> (mfirstToken is a std::string object.)
    >>
    >> if(m_firstToken.c_str()[0] == 'M' || m_firstToken.c_str().c_str()[0]
    >> == 'T')
    >> {
    >> // ... other code here
    >> }
    >>
    >> And of course my compiler barfs over it. But what it tells me is
    >> .c_str requires an object. Had to peer at that for like 20 minutes
    >> to spot the stupid mistake. I should not drink and code.
    >> Socks
    >>

    >
    > Drinking and posting also has it's hazards too.
    >
    > if (m_firstToken[0] == 'M' || m_firstToken[0] == 'T')
    > {
    > // ... other code here
    > }
    >
    > I assume m_firstToken is a std::string.
    >
    > Usually the best thing to do when parsing a string is to place the value
    > into a temporary. i.e.
    >
    > const char tmp = m_firstToken[0];
    >
    > if (tmp == 'M' || tmp == 'T')
    > {
    > // ... other code here
    > }


    Sorry, I must have missed something since I can't figure out why. Whould
    you care to explain?

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Feb 13, 2007
    #4
  5. red floyd wrote:
    ....
    > Took me forever to figure out why nothing was getting initialized, and
    > some memory was getting corrupted.


    I've seen this one more than once.

    assert( val = expression );
    return val;
     
    Gianni Mariani, Feb 14, 2007
    #5
  6. Gianni Mariani wrote:
    > red floyd wrote:
    > ...
    >> Took me forever to figure out why nothing was getting initialized,
    >> and some memory was getting corrupted.

    >
    > I've seen this one more than once.
    >
    > assert( val = expression );
    > return val;


    OK... How is that a syntax error? Essentially it's the same as

    for (int i = 1; i <= somearraysize; i+=1)
    { /* use 'i' to index a C array without subtracting 1 */ }

    i.e. lack of understanding what some constructs do or mean. It's
    all over the place in code brought over from PL/I or Fortran.
     
    Victor Bazarov, Feb 14, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    986
    Mark Goldin
    Jan 17, 2004
  2. SenthilVel
    Replies:
    0
    Views:
    962
    SenthilVel
    Jun 7, 2006
  3. Brandon McCombs
    Replies:
    4
    Views:
    525
    Richard Wheeldon
    Aug 28, 2006
  4. yawnmoth
    Replies:
    97
    Views:
    4,760
    Bent C Dalager
    Feb 27, 2009
  5. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,039
Loading...

Share This Page