Skip to content

Instantly share code, notes, and snippets.

@domiyanyue
domiyanyue / declaration.cpp
Last active April 25, 2020 17:39
declaration example
// example declaration
// declaration of a class
class MyClass;
// delaration of a function
int my_function (int arg);
// declaration of varibales, also contains definition
int my_var;
@domiyanyue
domiyanyue / definition.cpp
Created April 25, 2020 17:41
definition example
// example definition
// definition of a class
class MyClass {
private:
int m_size;
public:
MyClass(int n) : m_size(n){}
~MyClass() {}
};
@domiyanyue
domiyanyue / mutual_recursion.cpp
Created April 25, 2020 18:05
mutual recursion
bool is_even(int n);
bool is_odd(int n);
bool is_even(int n) {
if (n == 0)
return true;
else
return is_odd(n - 1);
}
example.cpp: In function ‘bool is_even(int)’:
example.cpp:8:16: error: ‘is_odd’ was not declared in this scope
return is_odd(n - 1);
^~~~~~
@domiyanyue
domiyanyue / class_example.cpp
Created April 25, 2020 18:48
class example for declaration
class House;
class Owner;
class House{
public:
Owner* my_owner;
House(Owner* owner){
my_owner = owner;
}
~House(){}
@domiyanyue
domiyanyue / class_example_output.txt
Created April 25, 2020 18:54
class example error
class_example.cpp:6:5: error: ‘Owner’ does not name a type
Owner* my_owner;
^~~~~
class_example.cpp:8:19: error: ‘Owner’ has not been declared
void setOwner(Owner* owner){
^~~~~
class_example.cpp: In member function ‘void House::setOwner(int*)’:
class_example.cpp:9:9: error: ‘my_owner’ was not declared in this scope
my_owner = owner;
^~~~~~~~
@domiyanyue
domiyanyue / declaration_1.cpp
Created April 25, 2020 19:04
declaration example 1
int a = 1;
int foo(){
return a;
}
@domiyanyue
domiyanyue / declaration_1.cpp
Last active April 25, 2020 19:13
declaration example 1
int bar() {
return 1;
}
int foo(){
return bar();
}
@domiyanyue
domiyanyue / main.cpp
Last active April 25, 2020 19:10
example include header declaration
#include "util.h"
int foo(){
return bar();
}
@domiyanyue
domiyanyue / example.cpp
Created April 25, 2020 19:13
error definition order
int foo(){
// can't find bar's definition
return bar();
}
int bar() {
return 1;
}