Skip to content

Instantly share code, notes, and snippets.

@domiyanyue
domiyanyue / header.h
Last active May 3, 2020 21:33
header time with guard
#ifndef TIME_H
#define TIME_H
struct Time {
int hour;
int minute;
};
void printTime(Time t);
#endif
@domiyanyue
domiyanyue / template.h
Created April 28, 2020 02:59
header file guard template
#ifndef A_UNIQUE_HEADER_FILE_GUARD_NAME
#define A_UNIQUE_HEADER_FILE_GUARD_NAME
// header file content
...
#endif
@domiyanyue
domiyanyue / struct.cpp
Created April 28, 2020 01:31
double definition struct
struct Time{
int hour;
int minutes;
};
struct Time{
int hour;
int minutes;
};
@domiyanyue
domiyanyue / processed_main.cpp
Created April 28, 2020 01:06
header guard after preprocessing main
...
// replaced from first #include "time.h"
struct Time {
int hour;
int minute;
};
...
// replaced from #include "util.h" which also include "time.h"
struct Time {
int hour;
@domiyanyue
domiyanyue / error.txt
Created April 28, 2020 01:02
harder guard error
$ g++ -c main.cpp
In file included from util.h:1:0,
from main.cpp:3:
time.h:1:8: error: redefinition of ‘struct Time’
struct Time {
^~~~
In file included from main.cpp:2:0:
time.h:1:8: note: previous definition of ‘struct Time’
struct Time {
^~~~
@domiyanyue
domiyanyue / main.cpp
Last active May 3, 2020 21:29
header guard example
#include <iostream>
#include "time.h"
#include "util.h"
int main(){
Time t;
t.hour = 2;
t.minute = 10;
printTime(t);
std::cout << "is Day time? " << isDayTime(t) << std::endl;
@domiyanyue
domiyanyue / 1_util.h
Last active May 3, 2020 21:27
example header guard util
#include "time.h"
bool isDayTime(Time d);
@domiyanyue
domiyanyue / 1_time.h
Last active May 3, 2020 21:27
example header guard date
struct Time {
int hour;
int minute;
};
void printTime(Time t);
@domiyanyue
domiyanyue / example.cpp
Created April 27, 2020 02:35
example struct enum compilation explain
struct Point {
int x;
int y;
};
enum Color { black = 0, white = 1, grey = 2};
Point p;
Color c = Color::black;
@domiyanyue
domiyanyue / 1_main.cpp
Created April 27, 2020 02:17
example_struct_header_2
#include "util.h"
int main(){
// set to 9:10
Time t(9, 10);
return isDayTime(t);
}