Oct 4, 2017 at 6:44pm UTC
I fixed it by replacing the h with a capital H "Dog.H"
surely that can't be what was causing the problem
my compiler is MinGW,I always thought the convention was to have a small h to represent a header file
Last edited on Oct 4, 2017 at 6:53pm UTC
Oct 4, 2017 at 6:57pm UTC
anyway while I'm this far,
how come we have to include the name space in the header file and the .cpp file?
wouldn't it be better convention if we could just put it in the header file?
Oct 4, 2017 at 9:14pm UTC
Well, the whole point of a namespace is to prevent ambiguous names from different libraries.
You can also define the function body like this:
1 2 3 4 5 6
#include "Dog.h"
void amc::Dog::speak()
{
std::cout << "BARK" << std::endl;
}
and use it like this
1 2 3 4 5
int main()
{
amc::Dog dog;
dog.speak();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// Example program
#include <iostream>
namespace a {
class Dog {
public :
void speak();
};
}
namespace b {
class Dog {
public :
void speak();
};
}
void a::Dog::speak()
{
std::cout << "BARK" << std::endl;
}
void b::Dog::speak()
{
std::cout << "MEOW?" << std::endl;
}
int main()
{
a::Dog dog1;
dog1.speak();
b::Dog dog2;
dog2.speak();
}
________________________________________________________________
This is an error because the Dog class is ambiguous:
1 2 3 4 5 6 7 8 9 10 11
int main()
{
using namespace a;
using namespace b;
Dog dog1;
dog1.speak();
Dog dog2;
dog2.speak();
}
Also adds to why "using namespace ___;" declarations are almost always bad.
Last edited on Oct 4, 2017 at 10:07pm UTC