In C++, I am trying to put two classes in separate files into the same namespace.
I get two errors in main
saying that the class Head
is not in the namespace Names
, and that days
is not declared in this scope.
It seems that when I switch the order of the #include
statements, the class that doesn't work flip flops between the two. From what I've read, the namespace should open again when the second file is included, and put the second class into the namespace, but it isn't working.
Why does the order of which one I include matter as to which works? What can I do to make both classes be a part of the same namespace, while keeping them in separate header files?
main.cpp
#include <iostream>
#include "dy.hpp"
#include "dayyear.hpp"
int main(){
int d=2;
int y=5;
Names::Dy yup(y,d);
Names::Head days(y,d);
days.print();
yup.print();
return 0;
}
dayyear.hpp
#ifndef DY_H
#define DY_H
namespace Names{
class Head{
int year;
int day;
public:
Head();
Head(int y, int d);
void print();
void change(int y, int d);
};
}
#endif
dayyear.cpp
#include <iostream>
#include "dayyear.hpp"
Names::Head::Head(){
day=0;
year=0;
}
Names::Head::Head(int y, int d){
day=d;
year=y;
}
void Names::Head::print(){
std::cout<<day<<", "<<year<<std::endl;
}
void Names::Head::change(int y, int d){
year=y;
day=d;
}
dy.hpp
#ifndef DY_H
#define DY_H
namespace Names{
class Dy{
int year;
int day;
public:
Dy();
Dy(int y, int d);
void print();
void change(int y, int d);
};
}
#endif
dy.cpp
#include <iostream>
#include "dy.hpp"
Names::Dy::Dy(){
day=0;
year=0;
}
Names::Dy::Dy(int y, int d){
day=d;
year=y;
}
void Names::Dy::print(){
std::cout<<day<<", "<<year<<std::endl;
}
void Names::Dy::change(int y, int d){
year=y;
day=d;
}