stupid syntax errors

P

Puppet_Sock

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
 
R

red floyd

Puppet_Sock said:
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.
 
G

Gianni Mariani

Puppet_Sock said:
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
}
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Puppet_Sock said:
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?
 
G

Gianni Mariani

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;
 
V

Victor Bazarov

Gianni said:
red floyd wrote:
...

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top