Strange problem with static member functions and namespaces

I have got a strange problem with static member functions.
Given the following code example:

TransmissionType.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef TRANSMISSIONTYPE_H_
#define TRANSMISSIONTYPE_H_

namespace myNamespace
{
	class TransmissionType
	{
	private:
		unsigned char _value;
		explicit TransmissionType(unsigned char value);

	public:
		static TransmissionType Read() { return TransmissionType(0x01); }
		static TransmissionType Write() { return TransmissionType(0x02); }
		inline unsigned char getValue() { return _value; }
	};
}
#endif /* TRANSMISSIONTYPE_H_ */


TransmissionType.cpp:

1
2
3
4
5
6
#include "TransmissionType.h"

namespace myNamespace
{
	TransmissionType::TransmissionType(unsigned char value) : _value(value) { }
}


CommunicationDirection.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef COMMUNICATIONDIRECTION_H_
#define COMMUNICATIONDIRECTION_H_

namespace myNamespace
{
	class CommunicationDirection
	{
	private:
		unsigned char _value;
		explicit CommunicationDirection(unsigned char value);

	public:
		static CommunicationDirection Forward() { return CommunicationDirection(0x00); }
		static CommunicationDirection Backward() { return CommunicationDirection(0x01); }
		inline unsigned char getValue() { return _value; }
	};
}

#endif /* COMMUNICATIONDIRECTION_H_ */ 


CommunicationDirection.cpp:

1
2
3
4
5
6
#include "CommunicationDirection.h"

namespace myNamespace
{
	CommunicationDirection::CommunicationDirection(unsigned char value) : _value(value) {  }
}


The classes above are used in the CommunicationFrameHeader class.

CommunicationFrameHeader.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef COMMUNICATIONFRAMEHEADER_H_
#define COMMUNICATIONFRAMEHEADER_H_

class CommunicationDirection;
class TransmissionType;

namespace myNamespace
{
	class CommunicationFrameHeader
	{
	private:
		static unsigned char fooBar(const CommunicationDirection& communicationDirection, const TransmissionType& transmissionType);
	};
}

#endif /* COMMUNICATIONFRAMEHEADER_H_ */ 


CommunicationFrameHeader.cpp:


1
2
3
4
5
6
7
8
9
10
11
12
#include "CommunicationFrameHeader.h"
#include "CommunicationDirection.h"
#include "TransmissionType.h"

namespace myNamespace
{
	unsigned char CommunicationFrameHeader::fooBar(const CommunicationDirection& communicationDirection, const TransmissionType& transmissionType)
	{
		CommunicationDirection direction = communicationDirection;
		return 0;
	}
}



If I compile the source code above, I get the following compiler error:


../CommunicationFrameHeader.cpp:8: error: prototype for `unsigned char myNamespace::CommunicationFrameHeader::fooBar(const myNamespace::CommunicationDirection&, const myNamespace::TransmissionType&)' does not match any in class `myNamespace::CommunicationFrameHeader'
subdir.mk:27: recipe for target `CommunicationFrameHeader.o' failed
../CommunicationFrameHeader.h:12: error: candidate is: static unsigned char myNamespace::CommunicationFrameHeader::fooBar(const CommunicationDirection&, const TransmissionType&)
make: *** [CommunicationFrameHeader.o] Error 1


The source code compiles without any error if I remove the namespace 'myNamespace' from all source files. So I think the problem is caused by the namespaces. But I don't know why, because all classes are in the same namespace. Any suggestions?

Thank you very much!
Last edited on
Problem solved, I had to move the class forward declarations into the namespace.

CommunicationFrameHeader.h:

1
2
3
4
5
6
namespace myNamespace
{
    class CommunicationDirection;
    class TransmissionType;
    
    [...]


Sorry for bothering you :-)
Your issue could possibly be cured by rearranging the inclusion order of
the include file in the CommunicationFrameHeader.cpp: file - but the real answer is to move the forward declaration for class CommunicationDirection; and
class TransmissionType; inside the namespace myNamespace brackets - like this:


communicationframeheader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace myNamespace
{
    //move the forward declaratins here.
     class CommunicationDirection;
     class TransmissionType;	

class CommunicationFrameHeader
	{
	private:
		static unsigned char fooBar(const CommunicationDirection& communicationDirection, const TransmissionType& transmissionType);
	};
}

#endif /* COMMUNICATIONFRAMEHEADER_H_ */  


This basically ensures that the compiler know that the classes being forward declared are part of the myNameSpace scope.

The way you have them at the moment, and the way your files include each other makes it look like they are in the GLOBAL namespace scope.
Yes, exactly. Thank you very much for your efforts, guestgulkan. You made my day :-)
Topic archived. No new replies allowed.