Jump to content



Photo

c++ Help


  • Please log in to reply
20 replies to this topic

#16 ILikeTobacco

ILikeTobacco

    Neowinian Senior

  • Joined: 08-July 10

Posted 09 September 2013 - 14:29

Pretty much why I walked away from C/C++ 2 decades ago. :p  Sure it's handy, but it's a pain in the backside, too!

Same. Gave up on C++ a long time ago. C# all the way. :p




#17 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 09 September 2013 - 22:32

At least when you mistype something in most programming languages, you tend to get something from the compiler like "hey what's that you typed in file (Z) at line (X) column ( Y), I have no idea what that is." In C++ you tend to get the equivalent of "AAAAAAaaaaarglgl.... guh." Much of the difficulty consists of learning how to map the gibberish to actual errors.

 
That is why I use Clang for my C and C++ work when possible: its error messages are so much more sane than GCC. In my opinion one of the biggest advantages of Visual Studio's C++ compiler over GCC is its error messages. That said, I think that Clang beats them both by a long shot with exceptionally helpful diagnostic messages. I was going to try to demonstrate Clang's superiority in this regard using the author's code, but it didn't actually make that much of a difference. For the record, the results are as follows:
$ make CXX=g++
g++ -c -std=c++11 -w -O3   Person.cpp -o Person.o
g++ -c -std=c++11 -w -O3   OCCCDate.cpp -o OCCCDate.o
g++ -c -std=c++11 -w -O3   TestOCCCDate.cpp -o TestOCCCDate.o
In file included from Person.h:7:0,
                 from TestOCCCDate.cpp:4:
OCCCDate.h:8:7: error: redefinition of ‘class OCCCDate’
In file included from TestOCCCDate.cpp:3:0:
OCCCDate.h:8:7: error: previous definition of ‘class OCCCDate’
make: *** [TestOCCCDate.o] Error 1

$ make CXX=clang++
clang++ -c -std=c++11 -w -O3   Person.cpp -o Person.o
clang++ -c -std=c++11 -w -O3   OCCCDate.cpp -o OCCCDate.o
clang++ -c -std=c++11 -w -O3   TestOCCCDate.cpp -o TestOCCCDate.o
In file included from TestOCCCDate.cpp:4:
In file included from ./Person.h:7:
./OCCCDate.h:8:7: error: redefinition of 'OCCCDate'
class OCCCDate{
      ^
./OCCCDate.h:8:7: note: previous definition is here
class OCCCDate{
      ^
In file included from TestOCCCDate.cpp:4:
In file included from ./Person.h:7:
./OCCCDate.h:15:12: error: expected member name or ';' after declaration specifiers
                OCCCDate();//default constructor, uses current system date and time
                ~~~~~~~~ ^
./OCCCDate.h:16:12: error: expected member name or ';' after declaration specifiers
                OCCCDate(int day, int month, int year);//as define above
                ~~~~~~~~ ^
./OCCCDate.h:16:12: error: expected ')'
./OCCCDate.h:16:11: note: to match this '('
                OCCCDate(int day, int month, int year);//as define above
                        ^
4 errors generated.
make: *** [TestOCCCDate.o] Error 1

$ g++ --version
g++ (Debian 4.7.2-5) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ clang++ --version
Debian clang version 3.0-6.2 (tags/RELEASE_30/final) (based on LLVM 3.0)
Target: x86_64-pc-linux-gnu
Thread model: posix

I can understand why Majesticmerc was able to spot the error whereas nether Clang nor GCC could catch it automatically. Therefore I have to agree with you to some extent. While some compilers do a much better job at printing helpful diagnostic messages than others, it is generally much more difficult to pinpoint errors in C++ than in newer languages like Python. I can generally interpret error messages and pinpoint the mistake in my C++ source code pretty quickly, but I think most of that comes down to experience. I remember Googling basically every error message when I started learning C++ hoping to find the resolution posted somewhere on the Internet. So while I would like to disagree with your assessment, I don't think I realistically can. Therefore I would merely like to point out that Clang prints much better diagnostic messages than other compilers - in most cases.

#18 fatbeerguy

fatbeerguy

    Neowinian

  • Joined: 12-September 13

Posted 12 September 2013 - 20:20

No, C++ is just ****. No decent programming language uses header files and silly macro tricks just to get programs to compile. Don't let your opinion of programming be tainted by the arcane and irrelevant rules of C++, even though it happens that this is what your teacher misleadingly thought would be a good idea to show you first. If your teacher thinks C++ is good just ask him "why are there header files in C++?" and watch him be embarassed. I fondly remember being unable to receive an intelligent answer to this question in my first programming class.

 

It ****es me off to think many people must quit programming simply because they're being shown the most bizarre and capricious language in existence first, and assume that all programming must be like that. It's not.

 

Arm yourself with courage and a good C++ book (I used The C++ Primer Plus by Stephen Prata), and when you're through with this course, take a look at such beautiful languages as Python, C#, F#, Scala, Rust, etc. I guarantee your outlook on programming will change for the better and dramatically so.

 

Seems a bit one sided :).

 

1. If you don't want "silly macro tricks" aka include guards then just never include the header more than once, or use #pragma once ;)

2. Header files are used to define the interface/contract. If you have a header which contains int get_magic_for_this_machine(); then you  call that function, it does some magic, you don't need to know about the 9000 other internal functions and classes it may have/use in its cpp file. Any code in a header file is inlined too. Also its possible to write something that only uses headers... but you'll have to endure huge build times.

3. For his problem there is no way the compiler could help him...

 

#ifdef A

#define B // Was this supposed to be A? or is B correct? What about the stuff below? It could mean anything so this could never be statically checked at build time

#define C

#define D

#endif



#19 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 12 September 2013 - 23:38

1. If you don't want "silly macro tricks" aka include guards then just never include the header more than once, or use #pragma once

 

Include guards are still the only standard way, and not including the header more than once in any compilation unit is a very difficult problem to solve in general.

 

2. Header files are used to define the interface/contract.

Not true, they also must include private class members, inline and template function definitions, among other implementation details. The way to define interfaces in object-oriented languages is to use actual interfaces.

 

Header files in C++ are just a legacy of the C compilation model; it's not coincidence that no other language makes use of them.
 

3. For his problem there is no way the compiler could help him...

I agree; the reliance of C and C++ on pre-processor tricks are but one of several issues that make the compiler and tooling very difficult to make user-friendly. The compiler can't report anything meaningful about macros because it doesn't know anything of macros. Well-designed languages take the feasability of tooling into consideration, which apparently wasn't on Stroustrup's mind back then.



#20 fatbeerguy

fatbeerguy

    Neowinian

  • Joined: 12-September 13

Posted 15 September 2013 - 13:30

Include guards are still the only standard way, and not including the header more than once in any compilation unit is a very difficult problem to solve in general.

 

Not true, they also must include private class members, inline and template function definitions, among other implementation details. The way to define interfaces in object-oriented languages is to use actual interfaces.

 

Header files in C++ are just a legacy of the C compilation model; it's not coincidence that no other language makes use of them.
 

I agree; the reliance of C and C++ on pre-processor tricks are but one of several issues that make the compiler and tooling very difficult to make user-friendly. The compiler can't report anything meaningful about macros because it doesn't know anything of macros. Well-designed languages take the feasability of tooling into consideration, which apparently wasn't on Stroustrup's mind back then.

 

For interfaces yes you could use something like

 

class IMyInterface 
{
public:

 virtual ~IMyInterface() { }
 virtual void the_magic() = 0;
};

But if you required data members you wished to be hidden then you can use the PIMPL idiom, but either way someone seeing some basic types or forward declared internal classes in the private section isn't too bad.



#21 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 15 September 2013 - 14:41

I know, my point is that header files are not interfaces or contracts, because they expose implementation details. Heck the entire STL is implemented in headers. They're a compiler convenience and a programmer inconvenience.