Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stupid syntax errors

Reply
Thread Tools

stupid syntax errors

 
 
Puppet_Sock
Guest
Posts: n/a
 
      02-13-2007
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

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      02-13-2007
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[i] here */
}
}

Took me forever to figure out why nothing was getting initialized, and
some memory was getting corrupted.
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      02-13-2007
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
}
 
Reply With Quote
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      02-13-2007
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
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      02-14-2007
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;

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-14-2007
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why does catching errors that aren't thrown give syntax errors? yawnmoth Java 97 02-27-2009 12:07 AM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM
Stupid is as Stupid Does! Michael P Gabriel Digital Photography 3 06-26-2004 12:49 PM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57