Inside FileThree.h
#ifndef FILETHREE
#define FILETHREE
namespace blue{}
class Filethree
{
public:
Filethree(void);
~Filethree(void);
};
#endif
Inside FileThree.cpp
#include "Filethree.h"
#include<iostream>
using namespace std ;
namespace blue{
void blueprint(int nVar){
cout<<"red::"<<nVar<<endl;
}
}
Filethree::Filethree(void)
{
}
Filethree::~Filethree(void)
{
}
Inside FileFour.h
#ifndef FILEFOUR
#define FILEFOUR
namespace red{}
class FileFour
{
public:
FileFour(void);
~FileFour(void);
};
#endif
Inside FileFour.cpp
#include "FileFour.h"
#include<iostream>
using namespace std;
namespace red{
void redprint(double nVar){
cout<<"red::"<<nVar<<endl;
}
}
FileFour::FileFour(void)
{
}
FileFour::~FileFour(void)
{
}
Inside main.cpp
#include "FileFour.h"
#include "Filethree.h"
using namespace red ;
using namespace blue ;
int main()
{
blueprint(12);
return 0;
}
When i compile the above file it gives me the following error .
error C3861: 'blueprint': identifier not found
Can anyone tell me why i am getting this error ?
Compiler can't find functions when they are not declared in header files.
You need to declare blueprint function in namespace blue in FileThree.h
FileThree.h:
namespace blue{
void blueprint(int nVar);
}
Same to redprint function, need to declare it in FileFour.h inside namespace red
FileFour.h
namespace red{
void redprint(double nVar);
}
When you separate your code into multiple files, you’ll have to use a namespace in the header and source file.
add.h
#ifndef ADD_H
#define ADD_H
namespace basicMath
{
// function add() is part of namespace basicMath
int add(int x, int y);
}
#endif
add.cpp
#include "add.h"
namespace basicMath
{
// define the function add()
int add(int x, int y)
{
return x + y;
}
}
main.cpp
#include "add.h" // for basicMath::add()
#include <iostream>
int main()
{
std::cout << basicMath::add(4, 3) << '\n';
return 0;
}
source : https://www.learncpp.com/cpp-tutorial/user-defined-namespaces/comment-page-4/#comment-464049
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With