I have a set up like this
file.h:
#pragma once
namespace a {
int home(double a, double b, ...
class b {
int la();
};
}
file.cpp
#include "file.h"
using namespace a;
int home(double a, double b, ...) {
//function stuff
}
int b::la() {
home(1, 2, ...)
}
And b is instantiated and used in main like this:
#include "file.h"
b instant;
instant.la()
But I have been getting this linker error everywhere where I am using the function home:
undefined reference to `a::home(double, double, ...)'
In function a::b::la()
I am pretty sure all of the CMakelists are correctly set up and everything is included.
But when I change the file.cpp to be in the namespace:
namespace a {
all of the same stuff
}
and it works just fine?
Any ideas why this is happening?
Your problem is with
using namespace a;up the top of your file.cpp. This is simply pulling in all the definitions fromnamespace ainto your code. Thus, when you defineint home(double, double, ...), you aren’t providing an implementation fora::home, you’re creating another function. You then haveint a::home(double, double, ...)andint home(double, double, ...).You either need
int a::home(double, double, ...)or to wrap everything in your.cppfile that’s undernamespace ainnamespace a { ... }.Edit: Your confusion stems from what a
usingdeclaration does. It simply pulls everything in from theanamespace and allows you to use it unqualified. It does not allow you to omit the qualification in definitions.