The code compile without error, I would like to know what's the different declaring extern function within and without namespace?
--a.h--
namespace mynamespace {
extern void foo();
}
--a.c--
namespace mynamespace {
void foo(){
dosomething;
};
}
Are there any concern that I should worry about when calling foo() without
specifying namespace?
#include "a.h"
int main(int argc char *argv )
foo()
}
instead of
#include "a.h"
int main(int argc char *argv )
mynamespace::foo();
}
It turns out that using namespace mynamespace; is already declared in other header file. So if one file include header file, which have already defined using namespace mynamespace;, the current header file will also affected.
Related
For eg., when using OpenCV, we specify
using namespace cv;
But where does C++ look down to know where it is defined?
using namespace will not make everything declared in that namespace visible. It will expose only what translation unit "sees".
Consider following code
One.h
#pragma once
namespace ns
{
void function1();
}
Two.h
#pramga once
namespace ns
{
void function2();
}
main.cpp
#include "Two.h" // <-- included only Two.h
using namespace ns;
int main()
{
function2(); // <-- is a ns::function2() located in Two.h
function1(); // <-- error compiler does not know where to search for the function
return 0;
}
What happened here is the compiler created translation unit with all preprocessor directives resolved. It will look something like this:
namespace ns
{
void function2();
}
using namespace ns;
int main()
{
function2(); // <-- OK. Declaration is visible
function1(); // <-- Error. No declaration
return 0;
}
How does C++ know where to look for the namespace specified using using namespace …?
It doesn't.
When you use
using namespace cv;
the scope of search for names (of classes, functions, variables, enums, etc) is expanded. The names are searched in the cv namespace in addition to other scopes in which they are normally searched.
Here is my main.cpp:
#include <iostream>
#include "function.cpp"
using namespace std;
extern int giveMain();
int main() {
int x = 4;
x = giveMain(x);
cout << x << endl;
}
And here is my function.cpp:
#include <iostream>
using namespace std;
int giveMain(int a) {
a = 3 + a;
return a;
}
But when I compile, it says that "Linker command failed". Can anyone helps me to solve this problem.
You declared the function int giveMain() in main.cpp but the function in function.cpp takes an int. Declare the correct function and it should work. Also extern is the default for functions so you don't need to include the keyword.
EDIT: Just noticed that you #include <function.cpp> in main.cpp. Never include .cpp files. The issue you were having was multiple definitions for int giveMain(int) because the contents of functions.cpp was being compiled twice.
In the small example needsExtern.cpp needs the definition of global::bar. needsExtern.cpp would normally include the file with the definition (in this case main.cpp). However, since the file is main.cpp it is not needed.
Why does needsExtern.cpp not need to include main.cpp?
needsExtern.h
struct NeedsExtern
{
NeedsExtern();
};
needsExtern.cpp
#include "needsExtern.h"
#include <iostream>
namespace global
{
extern const int bar;
}
NeedsExtern::NeedsExtern()
{
std::cout << global::bar << "\n";
}
main.cpp
#include "needsExtern.h"
namespace global
{
extern const int bar{26};
}
void main()
{
NeedsExtern ne;
}
This is precisely where extern is invented for: the compiler just assumes the variable is defined elsewhere in the project. You can read more about this principle here.
I've defined the following header file (in C), left out the function implementation since thise aren't needed:
#ifndef FFMPEG_MEDIAMETADATARETRIEVER_H_
#define FFMPEG_MEDIAMETADATARETRIEVER_H_
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/dict.h>
int setDataSource(AVFormatContext** pFormatCtx, const char* path);
#endif /*FFMPEG_MEDIAMETADATARETRIEVER_H_*/
In C++, I defined my second header file:
#ifndef MEDIAMETADATARETRIEVER_H
#define MEDIAMETADATARETRIEVER_H
using namespace std;
extern "C" {
#include "ffmpeg_mediametadataretriever.h"
}
class MediaMetadataRetriever
{
public:
MediaMetadataRetriever();
~MediaMetadataRetriever();
int setDataSource(const char* dataSourceUrl);
};
#endif // MEDIAMETADATARETRIEVER_H
In, mediametadataretriever.cpp I defined the following function:
int MediaMetadataRetriever::setDataSource(
const char *srcUrl)
{
// should call C function
AVFormatContext* pFormatCtx;
return setDataSource(&pFormatCtx, srcUrl);
}
When I try to compile this (C++) project in Eclipse I get a "No matching function call..." error related to:
return setDataSource(&pFormatCtx, srcUrl);
If I comment out the call, the code compiles fine:
int MediaMetadataRetriever::setDataSource(
const char *srcUrl)
{
return 0;
}
This appears to be a linking issue, does anyone know what I'm doing wrong?
setDataSource in that context is the name of the member function. To invoke the free function, try fully qualifying its name:
return ::setDataSource(&pFormatCtx, srcUrl);
// ^^
In my Function.h file:
class Function{
public:
Function();
int help();
};
In my Function.cpp file:
#include "Function.h"
int Function::help() //Error here
{
using namespace std;
cout << "Help";
return 1;
}
In my Main.cpp
#include <iostream>
#include "Function.h"
using namespace std;
int menu(){
Function fc;
fc.help();
return 1;
}
int main(int args, char**argv){
return menu();
}
Error is : ‘Function’ has not been declared
Can anybody tell me why? Thank you.
I tried like this and the problem is solved, but I dont really understand why:
In Function.h file:
I use
class Function{
public:
int status;
Function():status(1){}
int help();
};
instead of the old one
class Function{
public:
Function();
int help();
};
All your include statements are missing the #:
#include "Function.h"
^
Everything else looks fine, though you need to also #include <iostream> in Function.cpp since you're using cout.
Here is the Function.cpp that I got to compile and run:
#include "Function.h"
#include <iostream>
int Function::help() // No error here
{
using namespace std;
cout << "Help";
return 1;
}
Function::Function()
{
}
I had a similar problem. Make sure that you only have the required header files. I had two header files both including each other and it spit out this mistake.
You have created a declaration for the constructor of the Function class without including it in your implementation (cpp file).
#include "Function.h"
Function::Function(){
// construction stuff here
}
int Function::help() //Error here
{
using namespace std;
cout << "Help";
return 1;
}
In the first Function.h file you have declared the constructor but not defined it. In the second Function.h file (the one that works) you have defined and declared the Function constructor. You can either define and declare in the header or file, or declare in the header file and define in the Function.cpp file.
For example, declare in the header file "Function.h":
class Function
{
Function();
}
and define here in "Function.cpp":
Function::Function(){}
Or the alternative is to declare and define in the header file "Function.h":
Class Function
{
Function(){}
}
The other thing that you have done in the second version of the header file is to initialise the member variable "status" in the "member initialisation list" which is a good thing to do (See Effective C++ by Scott Meyers, Item 4). Hope this helps :)