expected initializer before 'speak'

hi guys this error in eclipse is baffling me,I have researched why this might be happening to me and can't see anything wrong with code yet I am getting this error

dog.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
#ifndef DOG_H_
#define DOG_H_

#include <iostream>

using namespace std;

namespace amc{

class Dog{

public:
    Dog();
	void speak();

};


}


dog.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


#include "Dog.h"

namespace amc{

Dog:: Dog(){


}


void Dog :: speak(){


}

}



thanks
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
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?

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
Also adds to why "using namespace ___;" declarations are almost always bad.

Indeed.

Your example at least limits the using into the scope of a function (main). Other functions in the same source file will not suffer from it.

The OP has a using (std) in a header file. A header file might be included by multiple source files. That is worse than just "bad".
Topic archived. No new replies allowed.