Reputation: 65
So I am currently making a small prototype for a bigger project I'm working on and I've gotten completely stuck on it. I am rather new to c++ and haven't worked with headers or namespaces before ever. The issue is that when i try to use my created namespace it fails completely and the compiler (clang) returns undefined.
#include <iostream>
#include "bark.hpp"
using namespace bark;
int main() {
bark::woof();
}
Header file:
#pragma once
#ifndef FUNCTIONS_HPP
#define FUNCTIONS_HPP
namespace bark {
void woof();
}
#endif
file with functions:
#include <iostream>
#include "bark.hpp"
void woof() {
std::cout << std::endl << "woof" << std::endl;
}
Upvotes: 2
Views: 130
Reputation: 60208
In the implementation file, this definition:
void woof()
{
// ...
}
defines a function woof
in the global namespace. So when you make the call:
bark::woof();
in the main
function, there is no bark::woof
defined, and the linker will refuse to link the program.
To correctly define the woof
from namespace bark
, you need to either qualify it:
void bark::woof()
{
// ...
}
or else introduce the namespace bark
, and define it inside:
namespace bark
{
void woof()
{
// ...
}
}
Upvotes: 6