473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Snipe hunting in C++

I just spent the better part of two hours trying to find a problem with my
code. The error messages had virtually nothing to do with the actual
problem. The problem, as it turned out, was a missing "}" to close a
namespace in a file I had moved from one namespace to another. This isn't
the first time something like this has happened. I guess after some time
I'll get better at knowing how to isolate such problems.

The worst thing about situations like this is that I start changing code to
see if it will correct the problem. That can introduce other problems that
persist after the original problems is identified and corrected. Do other
people run into situations like this?

To me, this is the hard part about working with C++. I can understand the
concepts of the language fairly well. I just waste a lot of time hunting
snipes.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #1
23 1510
Steven T. Hatton wrote:
I just spent the better part of two hours trying to find a problem with my
code. The error messages had virtually nothing to do with the actual
problem. The problem, as it turned out, was a missing "}" to close a
namespace in a file I had moved from one namespace to another. This isn't
the first time something like this has happened. I guess after some time
I'll get better at knowing how to isolate such problems.

The worst thing about situations like this is that I start changing code
to
see if it will correct the problem. That can introduce other problems
that
persist after the original problems is identified and corrected. Do other
people run into situations like this?

To me, this is the hard part about working with C++. I can understand the
concepts of the language fairly well. I just waste a lot of time hunting
snipes.


For this specific problem, whenever I open a {, I immediately close it.
Like:

void function(){}

and then I start filling in the body. It really helps avoiding such errors.
Jul 22 '05 #2
Lieven wrote:

For this specific problem, whenever I open a {, I immediately close it.
Like:

void function(){}

and then I start filling in the body. It really helps avoiding such
errors.


I do that when I create a namespace or a class. Especially when it comes to
classes I find it best to be sure I put the ';' at the end before I do
anything else. In the case I just described in my previous post, I was
refactoring, so the class and namespace already existed. I really thought
I had put the '}' at the end when I changed things. Perhaps I
inadvertently deleted it before I saved the file.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #3


Lieven wrote:
Steven T. Hatton wrote:
I just spent the better part of two hours trying to find a problem with my
code. The error messages had virtually nothing to do with the actual
problem. The problem, as it turned out, was a missing "}" to close a
namespace in a file I had moved from one namespace to another. This isn't
the first time something like this has happened. I guess after some time
I'll get better at knowing how to isolate such problems.

The worst thing about situations like this is that I start changing code
to
see if it will correct the problem. That can introduce other problems
that
persist after the original problems is identified and corrected. Do other
people run into situations like this?

To me, this is the hard part about working with C++. I can understand the
concepts of the language fairly well. I just waste a lot of time hunting
snipes.


For this specific problem, whenever I open a {, I immediately close it.
Like:

void function(){}

and then I start filling in the body. It really helps avoiding such errors.


he said he was in the middle of a code maintenance op tho... and it is *easy* to
get into such situations during maintanence.

David
Jul 22 '05 #4
You could use vi with syntax coloring.
Jul 22 '05 #5
filo wrote:
You could use vi with syntax coloring.

I'm currently using Emacs with font-lock-mode. Sure, it helps _a_lot_, but
it didn't help much in finding the problem. One main reason it was hard to
find was that the errors were pointing to other files that directly or
indirectly included the problematic file.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #6

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:6N********************@speakeasy.net...
I just spent the better part of two hours trying to find a problem with my
code. The error messages had virtually nothing to do with the actual
problem. The problem, as it turned out, was a missing "}" to close a
namespace in a file I had moved from one namespace to another. This isn't
the first time something like this has happened. I guess after some time
I'll get better at knowing how to isolate such problems.
This is definitely a familiar problem. I know that on my main compiler (VC7.1)
the error messages generated when there is a missing "}" give little indication
of what the real problem is. But after much experience writing ill-formed code
I've learned what these error messages look like, so I now start looking for the
missing "}" immediately.
The worst thing about situations like this is that I start changing code to
see if it will correct the problem. That can introduce other problems that
persist after the original problems is identified and corrected. Do other
people run into situations like this?


Just back everything up right before you know you are about to make a series of
major changes to track down the location of the missing "}". One you find it,
revert to the saved version and apply the change.

Jonathan
Jul 22 '05 #7
Steven T. Hatton wrote:
I just spent the better part of two hours trying to find a problem with my code.
The error messages had virtually nothing to do with the actual problem.
The problem, as it turned out, was a missing "}"
This is a flaw in the language design inherited from C.
Common lisp has a similar problem with parentheses.
The problem is that
the the bracket pairs (curly, square, angle) or parentheses
are almost invisible to the human eye.
Other languages use more visible 'begin' and 'end' keywords.
You might consider defining C preprocessor macros:

#define begin {
#define end }

and using them instead of brackets.

Misuse of the C style comment "brackets" '/*' and '*/"
also can introduce bugs that are extremely hard to track down.
to close a namespace in a file I had moved from one namespace to another.
This isn't the first time something like this has happened.
I guess [that] after some time
I'll get better at knowing how to isolate such problems.
Unfortunately, no.
Bugs introduced by typo's and paste-o's cause everyone grief.
The worst thing about situations like this is that I start changing code to
see if it will correct the problem. That can introduce other problems
that persist after the original problems is identified and corrected.
Do other people run into situations like this?
Have you considered using some sort of revision control system?

https://www.cvshome.org/
To me, this is the hard part about working with C++.
I can understand the concepts of the language fairly well.
I just waste a lot of time hunting snipes.


Being anal about a consistent style seems to help a little.
You can easily spot small departures
from the way you usually write code
and sometimes those departures are subtle bugs.

Jul 22 '05 #8

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message:
You might consider defining C preprocessor macros:

#define begin {
#define end }


This is funny. Think of what it would do to code using the STL.

Jonathan
Jul 22 '05 #9
Jonathan Turkanis wrote:
E. Robert Tisdale wrote:
You might consider defining C preprocessor macros:

#define begin {
#define end }


This is funny. Think of what it would do to code using the STL.


Try it.
Jul 22 '05 #10

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cm**********@nntp1.jpl.nasa.gov...
Jonathan Turkanis wrote:
E. Robert Tisdale wrote:
You might consider defining C preprocessor macros:

#define begin {
#define end }


This is funny. Think of what it would do to code using the STL.


Try it.


Okay.

Program text:

#include <vector>

#define begin {
#define end }

int main()
{
using namespace std;
vector<char> v(10, 'a');
for ( vector<char>::iterator first = v.begin(), last = v.end();
first != last;
++first )
{
*first = 'b';
}

}

Preprocessed output:

<snip>

int main()
{
using namespace std;
vector<char> v(10, 'a');
for ( vector<char>::iterator first = v.{(), last = v.}();
first != last;
++first )
{
*first = 'b';
}
}

Jonathan
Jul 22 '05 #11
Jonathan Turkanis wrote:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cm**********@nntp1.jpl.nasa.gov...
Jonathan Turkanis wrote:

E. Robert Tisdale wrote:
You might consider defining C preprocessor macros:

#define begin {
#define end }

This is funny. Think of what it would do to code using the STL.


Try it.

Okay.

Program text:

#include <vector>

#define begin {
#define end }

int main()
{
using namespace std;
vector<char> v(10, 'a');
for ( vector<char>::iterator first = v.begin(), last = v.end();
first != last;
++first )
{
*first = 'b';
}

}

Preprocessed output:

<snip>

int main()
{
using namespace std;
vector<char> v(10, 'a');
for ( vector<char>::iterator first = v.{(), last = v.}();
first != last;
++first )
{
*first = 'b';
}
}

Jonathan


Funny :) .
A very good counter-example to prove why macros always ought to be in
*all caps*.

--
Karthik. http://akktech.blogspot.com .
' Remove _nospamplz from my email to mail me. '
Jul 22 '05 #12
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:<2v*************@uni-berlin.de>...
<snip>
Just back everything up right before you know you are about to make a series > of
major changes to track down the location of the missing "}". One you find it,
revert to the saved version and apply the change.

Jonathan


Better. Use some kind of version control system.

Marcelo Pinto
Jul 22 '05 #13

"Marcelo Pinto" <mp****@brturbo.com> wrote in message
news:7a*************************@posting.google.co m...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:<2v*************@uni-berlin.de>... <snip>
Just back everything up right before you know you are about to make a series of
major changes to track down the location of the missing "}". One you find it, revert to the saved version and apply the change.

Jonathan


Better. Use some kind of version control system.


I'm not sure what you mean -- are you agreeing or disagreeing? I think my answer
is general enough to include using a version control system. (Personally I use
CVS.)
Marcelo Pinto

Jul 22 '05 #14
Jonathan Turkanis wrote:

[snip]
cat main.cc

#include <vector>

#define BEGIN {
#define END }

int main(int argc, char* argv[])
BEGIN
using namespace std;
vector<char> v(10, 'a');
for (vector<char>::iterator i = v.begin(); i != v.end(); ++i)
BEGIN
*i = 'b';
END
return 0;
END
Jul 22 '05 #15

"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:cm**********@nntp1.jpl.nasa.gov...
Jonathan Turkanis wrote:

[snip]
> cat main.cc

#include <vector>

#define BEGIN {
#define END }

int main(int argc, char* argv[])
BEGIN
using namespace std;
vector<char> v(10, 'a');
for (vector<char>::iterator i = v.begin(); i != v.end(); ++i)
BEGIN
*i = 'b';
END
return 0;
END


All caps is a must, but you also need a unique 'namespace' prefix. Furthermore,
while you're at it, you should correct some other language flaws. May I suggest

#include <vector>

#define PUNC_BEGIN_CURLY {
#define PUNC_END_CURLY }
#define PUNC_BEGIN_ROUND {
#define PUNC_END_ROUND }
#define PUNC_BEGIN_SQUARE {
#define PUNC_END_SQUARE }
#define PUNC_BEGIN_ANGLED {
#define PUNC_END_ANGLED }

int main
PUNC_BEGIN_ROUND
int argc, char* argv PUNC_BEGIN_SQUARE PUNC_END_SQUARE
PUNC_END_ROUND
PUNC_BEGIN_CURLY
using namespace std;
vector PUNC_BEGIN_ANGLED char PUNC_END_ANGLED v
PUNC_BEGIN_ROUND
10, 'a'
PUNC_END_ROUND;
for
PUNC_BEGIN_ROUND
vector PUNC_BEGIN_ANGLED char PUNC_END_ANGLED::iterator
i = v.begin PUNC_BEGIN_ROUND PUNC_END_ROUND;
i != v.end PUNC_BEGIN_ROUND PUNC_END_ROUND;
++i
PUNC_END_ROUND
PUNC_BEGIN_CURLY
*i = 'b';
PUNC_END_CURLY
return 0;
PUNC_END_CURLY

Jonathan
Jul 22 '05 #16
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:2v*************@uni-berlin.de...

#include <vector>

#define PUNC_BEGIN_CURLY {
#define PUNC_END_CURLY }
#define PUNC_BEGIN_ROUND {
#define PUNC_END_ROUND }
#define PUNC_BEGIN_SQUARE {
#define PUNC_END_SQUARE }
#define PUNC_BEGIN_ANGLED {
#define PUNC_END_ANGLED }

int main
PUNC_BEGIN_ROUND
int argc, char* argv PUNC_BEGIN_SQUARE PUNC_END_SQUARE
PUNC_END_ROUND
PUNC_BEGIN_CURLY
using namespace std;
vector PUNC_BEGIN_ANGLED char PUNC_END_ANGLED v
PUNC_BEGIN_ROUND
10, 'a'
PUNC_END_ROUND;
for
PUNC_BEGIN_ROUND
vector PUNC_BEGIN_ANGLED char PUNC_END_ANGLED::iterator
i = v.begin PUNC_BEGIN_ROUND PUNC_END_ROUND;
i != v.end PUNC_BEGIN_ROUND PUNC_END_ROUND;
++i
PUNC_END_ROUND
PUNC_BEGIN_CURLY
*i = 'b';
PUNC_END_CURLY
return 0;
PUNC_END_CURLY

Jonathan


Have I died and gone to COBOL? :-)

-Mike
Jul 22 '05 #17
Jonathan Turkanis wrote:

[snip]
cat main.cc

#include <vector>

#define BEGIN {
#define END }
#define FOR for (
#define DO )

int main(int argc, char* argv[]) BEGIN
using namespace std;
vector<char> v(10, 'a');
FOR vector<char>::iterator i = v.begin(); i != v.end(); ++i DO
BEGIN
*i = 'b';
END
return 0;
END
Jul 22 '05 #18
Mike Wahler wrote:
Have I died and gone to COBOL? :-)


Should be:

Have I died and gone to COBOL? :-(
Jul 22 '05 #19
E. Robert Tisdale wrote:
Steven T. Hatton wrote:
I just spent the better part of two hours trying to find a problem with
my code. The error messages had virtually nothing to do with the actual
problem. The problem, as it turned out, was a missing "}"
This is a flaw in the language design inherited from C.
Common lisp has a similar problem with parentheses.
The problem is that
the the bracket pairs (curly, square, angle) or parentheses
are almost invisible to the human eye.
Other languages use more visible 'begin' and 'end' keywords.
You might consider defining C preprocessor macros:

#define begin {
#define end }

and using them instead of brackets.


That really would have done little in this case. What helped was C-x C-h
C-M-\, and then scrolling to the end of the file.
Misuse of the C style comment "brackets" '/*' and '*/"
also can introduce bugs that are extremely hard to track down.


Fortunately, font-lock-mode catches those for me.
to close a namespace in a file I had moved from one namespace to another.
This isn't the first time something like this has happened.
I guess [that] after some time
I'll get better at knowing how to isolate such problems.


Unfortunately, no.
Bugs introduced by typo's and paste-o's cause everyone grief.
The worst thing about situations like this is that I start changing code
to
see if it will correct the problem. That can introduce other problems
that persist after the original problems is identified and corrected.
Do other people run into situations like this?


Have you considered using some sort of revision control system?

https://www.cvshome.org/

That's only marginally helpful. First off, I need to be sure I make the
appropriate adds and commits before I introduce such a bug. Even if I do,
I am then required to pick through what I subsequently did to identify the
changes I really didn't want. If I leave all my files open while hunting
such a bug, then I can roll them back with C-M-_. But it's still a pain.

To me, this is the hard part about working with C++.
I can understand the concepts of the language fairly well.
I just waste a lot of time hunting snipes.


Being anal about a consistent style seems to help a little.
You can easily spot small departures
from the way you usually write code
and sometimes those departures are subtle bugs.


And that makes switching between editors a real pain. I depend on the hard
indentation in Emacs. Other editors my simulate such a feature in one way
or another, but usually with some adverse consequence.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #20
Steven T. Hatton wrote:


That really would have done little in this case. What helped was C-x C-h
C-M-\, and then scrolling to the end of the file.


Whoops! make that C-x, h, C-M-_.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #21
In message <2v*************@uni-berlin.de>, Jonathan Turkanis
<te******@kangaroologic.com> writes

All caps is a must, but you also need a unique 'namespace' prefix. Furthermore,
while you're at it, you should correct some other language flaws. May I suggest

#include <vector>

#define PUNC_BEGIN_CURLY {
#define PUNC_END_CURLY }
#define PUNC_BEGIN_ROUND {
#define PUNC_END_ROUND }
#define PUNC_BEGIN_SQUARE {
#define PUNC_END_SQUARE }
#define PUNC_BEGIN_ANGLED {
#define PUNC_END_ANGLED }

int main
PUNC_BEGIN_ROUND
int argc, char* argv PUNC_BEGIN_SQUARE PUNC_END_SQUARE
PUNC_END_ROUND
PUNC_BEGIN_CURLY
using namespace std;
vector PUNC_BEGIN_ANGLED char PUNC_END_ANGLED v
PUNC_BEGIN_ROUND
10, 'a'
PUNC_END_ROUND;
for
PUNC_BEGIN_ROUND
vector PUNC_BEGIN_ANGLED char PUNC_END_ANGLED::iterator
i = v.begin PUNC_BEGIN_ROUND PUNC_END_ROUND;
i != v.end PUNC_BEGIN_ROUND PUNC_END_ROUND;
++i
PUNC_END_ROUND
PUNC_BEGIN_CURLY
*i = 'b';
PUNC_END_CURLY
return 0;
PUNC_END_CURLY


Could do better. See: http://www.cotse.net/users/jeffrelf/X.CPP

--
Richard Herring
Jul 22 '05 #22
Steven T. Hatton wrote:
E. Robert Tisdale wrote:
Have you considered using some sort of revision control system?

https://www.cvshome.org/


That's only marginally helpful.


It was meant to answer the other part of your question,
"Do other people run into situations like this?"

Of course they do and it's much more serious
if you are a member of a team maintaining the same source repository.
Some kind of revision control system is essential
if you must work with other programmers on the same code.
Jul 22 '05 #23
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:<2v*************@uni-berlin.de>...
"Marcelo Pinto" <mp****@brturbo.com> wrote in message
news:7a*************************@posting.google.co m...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message

news:<2v*************@uni-berlin.de>...
<snip>
Just back everything up right before you know you are about to make a series of major changes to track down the location of the missing "}". One you find it, revert to the saved version and apply the change.

Jonathan


Better. Use some kind of version control system.


I'm not sure what you mean -- are you agreeing or disagreeing? I think my answer
is general enough to include using a version control system. (Personally I use
CVS.)
Marcelo Pinto


I thought you were saying making copies of all the files... :(
I also use CVS in my projects.

Marcelo Pinto
Jul 22 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sean | last post by:
Ok - I need: A Free Host (unlimited MYSQL - thus I can test all the stuff I want) A good PHP editor..Currently using PHPedit..don't like it. The history of PHP and it's meanings. Help
10
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to...
1
by: Hollywood | last post by:
Hello dear membres of the comp.unix.programmer. Please , I've got the following question to submit and I need your help. Here below you'll find a lexical and a syntaxic analysers. The lexical...
0
by: kesterson.al | last post by:
hunting unlimited 3 crack http://cracks.00bp.com F R E E
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.