-1

I've only recently got started with C++ and tried to build a simple class using different files (main, header and implementation). While doing that, I ran into a very strange (well, for me, at least) problem with #ifndef / #define. I'll show the code first:

// main.cpp :
#include <iostream>
#include "daytime.h"

using namespace std;

int main(int argc, char** argv) {
    DayTime sT = DayTime(75);
    cout << sT.getMin();

    return 0;
}

// daytime.h :
#ifndef DayTime
#define Daytime

class DayTime
{
    int min;

    public:
        DayTime(int min);
        int getMin();
        int getHour();
};

#endif

// daytime.cpp :
#include "daytime.h"

DayTime::DayTime(int min)
    : min(min) {}

DayTime::getMin()
{
    return this->min;
}

DayTime::getHour()
{
    return int (this->min / 60);
}

As you can see, there is a typo in #define Daytime in daytime.h - 'time' is written with a lowercase 't'. And here comes the strange part: as long as that typo is there, the program compiles and runs without problems. But the moment I correct it to #define DayTime, with uppercase 'T', it no longer complies and I get a multitude of errors:

2   0   main.cpp    In file included from main.cpp
9   11  daytime.h   [Error] expected unqualified-id before 'int'
9   11  daytime.h   [Error] expected ')' before 'int'
5   1   daytime.h   [Error] an anonymous struct cannot have function members
12  1   daytime.h   [Error] abstract declarator '<anonymous class>' used as declaration
D:\Dokumentumok\Prog\C++\Test\main.cpp  In function 'int main(int, char**)':
7   10  main.cpp    [Error] 'sT' was not declared in this scope
28      Makefile.win    recipe for target 'main.o' failed

I've been wasting hours on this by now, but I've always got back to this same issue: typo there, it works, typo gone, it doesn't - and I just don't get it. So I'd be very grateful for some assistance here.

0

1 Answer 1

3

#define DayTime causes any future occurrences of DayTime to be replaced with the empty string.

So the name of your class in class DayTime (and its constructor) are getting deleted, causing issues: the compiler effectively sees:

class
{
    int min;

    public:
        (int min);
        int getMin();
        int getHour();
};

Usually for this exact reason, when people make these include guards, they use something that is unlikely to show up as a real identifier. For example:

#ifndef DAYTIME_H
#define DAYTIME_H

class DayTime {
  // blah blah blah
};

#endif
6
  • Indeed. Usually people would define all caps markers to keep track of includes, like this: #define _DAYTIME_H
    – Dan
    May 1, 2019 at 8:49
  • #pragma once is a widely accepted alternative guard.
    – Andy Brown
    May 1, 2019 at 8:49
  • 1
    @AndyBrown I thought about suggesting #pragma once, but felt that it didn't illustrate the real point of my answer which is how #define Foo causes future uses of Foo to disappear. So I left it off in order to not be confusing. May 1, 2019 at 8:52
  • @AndyBrown: Widely supported, but non-standard.
    – einpoklum
    May 1, 2019 at 8:52
  • 2
    @Dan -- names that begin with an underscore followed by a capital letter (_DAYTIME_H) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. This is not a standard convention for include guard names. You may well see it in the implementation of the standard library, which uses reserved identifiers in many places. Those identifiers are reserved so that the standard library doesn't step on user code and so that user code doesn't step on the standard library. May 1, 2019 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.