• 0

c++ Help


Question

20 answers to this question

Recommended Posts

  • 0

From what I understand, you want to add a  date of birth into the Person class.

 

Step 1

To begin, you'll need to add a date of birth field into the person class. You'll need to do this in two parts.

 

First, you need to include the OCCCDate class header file inside your Person.h header file...

 

Person.h
---------------------------------------------------------

//Person.h

#ifndef PERSON_H
#define PERSON_H
#include<string>

// Add the OCCCDate.h header file...
#include "OCCCDate.h"

...

Once you've added the header file, you can create a private class member inside your person class like you've described in your original post.

 

Person.h
---------------------------------------------------------

...

class Person{
    private:
        string firstname;
        string lastname;

        // Add an OCCCDate class here as a member, just like you did for the strings above.

    public:

...    
Step 2:

It's unlikely that you'll want a person to have the current system date as their date of birth, so you'll need to add a parameter to the Person constructor to pass a date of birth. like this:

Person.h
---------------------------------------------------------

...

class Person{

    private:
        ...

    public:
        Person(string firstName, string lastName, OCCCDate dateOfBirth);
//                                                ^--------,---------^
//                                                         New parameter in the constructor.

...    
Don't forget you'll need to copy the date of birth parameter in the constructor in your Person.cpp file too! Looking at your .cpp file, you're almost there already :)).

Once you've done that, you can add a method to the Person class to retrieve the date of birth (just like Person::getFirstName() and Person::getLastName()), and you're good to go!

Hope this helps :)

Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595929151
Share on other sites

  • 0

I assume you're seeing an error talking about "redefinition of class OCCCDate"? That's because of the following lines in OCCCDate.h...

 

//file OcccDate.h

#ifndef OCCCDATE_H
#define OCCCDDATE_H
See the spelling mistake in the #define line (hint: two 'D's in DDATE)? You'll need to fix that like this...

 

//file OcccDate.h

#ifndef OCCCDATE_H
#define OCCCDATE_H
Once you fix that, it should work. Because of the spelling mistake, the #ifndef block was always TRUE (because OCCCDATE_H was never defined), and the compiler tried to define the class again, which is bad ;).
Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595930801
Share on other sites

  • 0

Or you could just use #pragma once rather than #ifndef #define #endif. All major compilers (MSVC, GCC, Clang, Intel, etc.) support it and it reduces the potential for bugs. Like the one you just had.

While I agree with you in practice, if it's an assignment it may be that the tutor is sticking strictly to the standard, and using #pragma vs a standard-compliant alternative may lose the student points.

WOW just that small mistake and it took me 3 days. jesus. programming is so hard.

Indeed it is, but its also incredibly gratifying if you enjoy it. Glad your issue is sorted. :)

Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595931131
Share on other sites

  • 0

WOW just that small mistake and it took me 3 days. jesus. programming is so hard.

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.

Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595931753
Share on other sites

  • 0

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.

 

 

That said, he didn't exactly help himself with that function name. VERY easy to mistype that, so I'm not at all surprised he didn't spot it...

Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595931945
Share on other sites

  • 0

That said, he didn't exactly help himself with that function name. VERY easy to mistype that, so I'm not at all surprised he didn't spot it...

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.

Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595932521
Share on other sites

  • 0

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.

 

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!

Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595932537
Share on other sites

  • 0

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.

Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595933651
Share on other sites

  • 0

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

Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595941813
Share on other sites

  • 0
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.

Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595942153
Share on other sites

  • 0

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.

Link to comment
https://www.neowin.net/forum/topic/1175399-c-help/#findComment-595947709
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Microsoft releases major feature updates for stock Windows 11 apps by Taras Buria In addition to releasing new Windows 11 preview builds, Microsoft announced that inbox Windows apps now have dedicated release notes in the official documentation. At long last, users have access to all the release notes for each app, with changes listed in chronological order. Microsoft used to announce feature updates for stock apps with each build. Now, with Windows Insider release notes hosted on the Microsoft Learn website, each app has a dedicated space for its changelog, which is very useful for those who want to track new features and improvements. Alongside that, Microsoft dropped massive feature updates for six stock apps: Clock, Media Player, Calculator, Voice Recorder, Photos, and Paint. Each app packs quite a lot of changes and new capabilities, so here are the release notes. Here are quick notes so that you can jump to the app you are interested in the most: Calculator Camera Clock Media Player Paint Photos Sound Recorder Here is what is new for the Calculator in version 11.2605.9.0: More accurate square-root results — Fixed rare cases where a calculation that should equal zero (like sqrt(2.25) - 1.5) returned a tiny leftover value instead. Readable text in High Contrast themes — Settings text now shows the correct colors in the High Contrast Aquatic and Desert themes. Fixed layout for right-to-left languages — For languages like Arabic and Hebrew, the graph, number pad, equation fields, and scroll buttons now appear correctly oriented. Reliable launch after upgrading — Fixed an issue where upgrading from much older versions could leave outdated settings that stopped the app from opening. Here is what is new for the Camera app (version 2026.2605.7.0): Zoom slider works on more cameras — The zoom slider now works on the latest cameras, respects your system zoom settings, and updates instantly when you change those settings. Full range of zoom levels — Fixed an issue where the zoom slider only showed three steps on some devices that zoom in finer increments. Front camera works on more devices — Resolved a problem that blocked the front-facing camera on certain wide-angle devices. More video resolution choices — You can now pick video resolutions that were previously hidden; the app shows a heads-up warning instead of removing them. QR links you can still use — When a scanned QR code points to something with no matching app, the link is now copied to your clipboard (with a notification) while still offering a Store search. Smarter default settings — When you haven't set a preference, the app now follows your system settings by default. The Clock app has a massive changelog with the following improvements in version 11.2605.9.0: Timers keep counting after they hit zero — When a timer runs out, it now keeps counting up (for example, -00:27:31) so you can see how far past the time you've gone. You can turn off the daily goal — Focus Sessions now include an "Off" option so you can skip setting a daily goal entirely. New 15-minute snooze option — Alarms now offer a 15-minute snooze interval. Run up to 3 countdowns at once — The Countdown Widget now supports three simultaneous countdowns, up from two. Timer Widget notifications now appear — Fixed an issue where the "timer finished" notification didn't show when the timer was started from the widget. Less clutter in Focus Sessions — Tasks you've already completed no longer show up in the Focus Session task list. More accurate focus progress — Fixed a rounding issue that could show your daily focus progress as a minute short (for example, 49 minutes instead of 50). Smoother World Clock comparisons — The World Clock compare page now loads dates as you scroll, so it feels more responsive. Up-to-date World Clock locations — Refreshed country and city names to match their current names. Correct sun and moon icons during midnight sun — Fixed an icon that wrongly showed a moon during all-day daylight in polar regions. Fixed back-button behavior in clock comparisons — Pressing back once now takes you back as expected, instead of jumping the date to 1926. Corrected the Newfoundland time zone — Newfoundland now uses the right time zone (St. John's). Disabled alarms stay looking disabled — Editing a turned-off alarm no longer makes it appear turned on. Cleaner timer cards — The expand button is now turned off on timer cards that have no time set, preventing actions that wouldn't do anything. Clearer theme setting — Updated the wording to "Choose your preferred app theme." Smoother Settings links — The "About" links in Settings no longer trigger an unexpected "switch apps" prompt. Fixed spacing in Spotify settings — Corrected uneven spacing in the Spotify settings card. Better focus visibility in High Contrast — The focus highlight in World Clock is now clearly visible in the High Contrast Aquatic and Desert themes. No more double announcements — Screen readers no longer read the timer value twice. Countdown names read correctly — Screen readers now properly announce the name of each countdown. Keyboard focus stays put — Focus no longer disappears after you press the Timer Reset button. Clearer alarm toggle for screen readers — Tidied up how the alarm on/off switch is announced. The Media Player app received plenty of changes as well (version 11.2605.14.0): Custom captions — You can now personalize how closed captions appear, with caption styling tied to your Windows caption settings, plus a quick link to open those settings directly. "Indexing" banner in the play queue — When your media library is still being scanned, a banner now explains why some items may not appear yet. Fixed the look of selected items — Corrected a layout glitch with selected items in lists. Fewer playback failures — Improved how the app recognizes supported file types, so more files play without issues. Playlists need a name — You can no longer accidentally save a playlist with a blank name. Cleaner look for empty playlists — Improved how a playlist appears when it has no items yet. More stable play queue edits — Fixed a crash that could happen when changing the play queue while the app was switching between sessions. Clearer "missing codec" message — Improved the dialog that appears when a file needs a codec you don't have, with clearer guidance on what to do. A big update is also available for Paint in version 11.2605.61.0: Adjustable eraser transparency — You can now control how transparent the eraser is. Cleaner stamp brush strokes — Fixed visible color shifts and artifacts when using stamp-style brushes. JPEG photos save in place — Opening a rotated JPEG and pressing Save now overwrites the original instead of unexpectedly prompting "Save As." No more crash on bad image files — Opening a damaged or invalid image, from within the app, by double click, or commandline, now shows a clear error message instead of closing the app. Classic selection behavior restored — The selection outline now hides while you move, resize, or rotate a selection, just like in classic Paint. Tidier AI image panel — Fixed missing spacing at the bottom of the AI image generation panel for a cleaner layout. Visible button hover in light theme — Toolbar split buttons now show a clear hover highlight in the light theme. Snappier toolbar — Streamlined how the ribbon lays out, giving a small speed boost at startup. Fewer background crashes — Fixed a crash that could happen while background tasks were finishing up. Stable app shutdown — Prevented rare crashes when closing the app. Fixed layer removal glitch — Deleting the active layer no longer leaves the layers list in an inconsistent state. Here is what is new in the Photos app (version 2026.11060.2004.0): AI watermarking — AI-generated or edited images can now carry a visible Copilot watermark. You choose Never, Always, or Ask Every Time in Settings, with a confirmation when saving. The watermarking is off by default in settings. Better viewing of small images and pixel art — Tiny images (like 16×16 pixel art) now zoom in far more to fill the screen and stay crisp instead of looking blurry. Select scanned text with the keyboard — When text is detected in an image, you can now navigate and select it using the arrow keys, Shift+Arrow, Home/End, and Ctrl+A, with a clear focus highlight. Fixed a crash in text recognition — Resolved a crash that could close Photos while detecting text in images; the app now recovers gracefully. Easier keyboard navigation — Tabbing through the navigation bar no longer stops on hidden controls, so it takes a single Tab to move past it instead of three. And finally, here is the Sound Recorder (version 11.2605.1.0): Waveform shows with Bluetooth mics — The live waveform now displays correctly when you record using a Bluetooth audio device. No more stray scrollbar — A non-working horizontal scrollbar no longer appears at the bottom of the waveform unless you've zoomed in. Mark button ready right away — The Mark button no longer looks grayed out until you hover over it after opening the app. Markers hidden for WAV files — Markers are now turned off for WAV recordings, since that format can't store them — so they're no longer lost silently. Smoother deleting — Quickly pressing Delete and Enter to remove several recordings in a row no longer triggers a "file doesn't exist" error. Fixed a memory issue — Resolved a memory leak that occurred each time a recording started. You can find all these changelogs in the official documentation here.
    • again, an article about Microsoft Edge and ridicules hater's comments
    • From this very same article: "For organizations that prefer a “more deliberate pace”, the Extended Stable channel remains an option."
    • Or every other browser, because they all behave the same, at least the mainstream ones. Firefox does exactly the same: background updates, restart to install them. Haters gotta hate, I guess.
  • Recent Achievements

    • Very Popular
      AndrewSteel earned a badge
      Very Popular
    • Veteran
      Taliseian went up a rank
      Veteran
    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Week One Done
      Timaximus earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      494
    2. 2
      PsYcHoKiLLa
      166
    3. 3
      +Edouard
      162
    4. 4
      Steven P.
      86
    5. 5
      ATLien_0
      77
  • Tell a friend

    Love Neowin? Tell a friend!