473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Dividing a namespace into declaration and elaboration parts

I have this namespace which works fine if I put everything in the header file:

Expand|Select|Wrap|Line Numbers
  1. namespace util
  2. {
  3.  
  4.     float getFloat(char* buf, int from);
  5.     bool  getBool(char* buf, int from);
  6.     short getShort(char* buf, int from);
  7.  
  8.  
  9. float getFloat(char* buf, int from)
  10. {
  11.     buf+=from;
  12.  
  13.     float tempVal;
  14.     memcpy(&tempVal, buf , sizeof(float));
  15.  
  16.     return tempVal;
  17. }
  18.  
  19. bool getBool(char* buf, int from)
  20. {
  21.     buf+=from;
  22.  
  23.     bool tempVal;
  24.     memcpy(&tempVal, buf , sizeof(bool));
  25.  
  26.     return tempVal;
  27. }
  28.  
  29. short getShort(char* buf, int from)
  30. {
  31.     buf+=from;
  32.  
  33.     bool tempVal;
  34.     memcpy(&tempVal, buf , sizeof(bool));
  35.  
  36.     return tempVal;
  37. }
  38.  
  39. } // namespace util
  40.  
I don't feel comfortable doing it this way and would rather put the elaboration code in a separate file. Nothing I tried has worked.

Can the namespace be divided up, or must I leave it alone?

Thanks in advance.
Feb 9 '07 #1
9 2117
AdrianH
1,251 Expert 1GB
I don't feel comfortable doing it this way and would rather put the elaboration code in a separate file. Nothing I tried has worked.

Can the namespace be divided up, or must I leave it alone?

Thanks in advance.
There is good reason why you shouldn't feel comfortable. This will not work if you include the header file more than once in your source code as it will generate multiple instances of the functions which will then conflict with each other.

Yes the namespace can be divided up. It can be declared as many times as you want. Every time you declare a namespace with the same name, it will add whatever you put within the namespace block into the namespace that you specify.

I.e.
Header file:
Expand|Select|Wrap|Line Numbers
  1. namespace util
  2. {
  3.  
  4.     float getFloat(char* buf, int from);
  5.     bool  getBool(char* buf, int from);
  6.     short getShort(char* buf, int from);
  7. }
  8.  
Source file:
Expand|Select|Wrap|Line Numbers
  1. namespace util
  2. {
  3.  
  4. float getFloat(char* buf, int from)
  5. {
  6.     buf+=from;
  7.  
  8.     float tempVal;
  9.     memcpy(&tempVal, buf , sizeof(float));
  10.  
  11.     return tempVal;
  12. }
  13.  
  14. bool getBool(char* buf, int from)
  15. {
  16.     buf+=from;
  17.  
  18.     bool tempVal;
  19.     memcpy(&tempVal, buf , sizeof(bool));
  20.  
  21.     return tempVal;
  22. }
  23.  
  24. short getShort(char* buf, int from)
  25. {
  26.     buf+=from;
  27.  
  28.     bool tempVal;
  29.     memcpy(&tempVal, buf , sizeof(bool));
  30.  
  31.     return tempVal;
  32. }
  33.  
  34. } // namespace util
  35.  
Alternatively, you can specify it in absolute terms like so:
Source file:
Expand|Select|Wrap|Line Numbers
  1. float util::getFloat(char* buf, int from)
  2. {
  3.     buf+=from;
  4.  
  5.     float tempVal;
  6.     memcpy(&tempVal, buf , sizeof(float));
  7.  
  8.     return tempVal;
  9. }
  10.  
  11. bool util::getBool(char* buf, int from)
  12. {
  13.     buf+=from;
  14.  
  15.     bool tempVal;
  16.     memcpy(&tempVal, buf , sizeof(bool));
  17.  
  18.     return tempVal;
  19. }
  20.  
  21. short util::getShort(char* buf, int from)
  22. {
  23.     buf+=from;
  24.  
  25.     bool tempVal;
  26.     memcpy(&tempVal, buf , sizeof(bool));
  27.  
  28.     return tempVal;
  29. }
  30.  
Hope this helps,


Adrian
Feb 9 '07 #2
There is good reason why you shouldn't feel comfortable. This will not work if you include the header file more than once in your source code as it will generate multiple instances of the functions which will then conflict with each other.

Yes the namespace can be divided up. It can be declared as many times as you want. Every time you declare a namespace with the same name, it will add whatever you put within the namespace block into the namespace that you specify.

Adrian
Adrain:

I tried both methods you suggested, putting elaboration code in a .cpp file. It compiled fine, but failed to link. There must be some step I am leaving out.
Feb 12 '07 #3
AdrianH
1,251 Expert 1GB
Adrain:

I tried both methods you suggested, putting elaboration code in a .cpp file. It compiled fine, but failed to link. There must be some step I am leaving out.
Please specify your compiler and what version it is. Also indicate how you are compiling this.


Adrian
Feb 12 '07 #4
Please specify your compiler and what version it is. Also indicate how you are compiling this.


Adrian
I am using the gnu complier for the PPC604 which creates a partially linked object. Final linking happens during load. The IDE is Eclipse 3.1.2 using Wind River Systems Workbench 2.5b plugins. The target system uses vxWorks 6.3.
Feb 12 '07 #5
AdrianH
1,251 Expert 1GB
I am using the gnu complier for the PPC604 which creates a partially linked object. Final linking happens during load. The IDE is Eclipse 3.1.2 using Wind River Systems Workbench 2.5b plugins. The target system uses vxWorks 6.3.
VxWorks, eh? I used to make fun of it and say it didn't work two jobs ago. Well, they had a few issues with their header files not being up to date (constness was one that really bugged me). And that Tornado environment! ARGH! To be fair, I was using the old crap because of my employer and was slowly started to move towards the new stuff only because WindRiver issued end of life statments. :) Yeah, the good ol' days... 6 months ago. :D Anyway, enough reminicing.

I tried it under Cygwin gnu g++ 3.4.4 compiler using Eclipse 3.2.1 with a managed makefile project (so it does all the dependency checking for me). I separated it into two files, main.cpp and main.h. What follows is the contents of both:

main.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef MAIN_H_
  2. #define MAIN_H_
  3. namespace util
  4. {
  5.  
  6.     float getFloat(char* buf, int from);
  7.     bool  getBool(char* buf, int from);
  8.     short getShort(char* buf, int from);
  9. }
  10. #endif /*MAIN_H_*/
  11.  
main.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "main.h"
  2. #include <memory.h>
  3. #if 0
  4. namespace util
  5. {
  6.     short getShort(char* buf, int from)
  7.     {
  8.         buf+=from;
  9.  
  10.         bool tempVal;
  11.         memcpy(&tempVal, buf , sizeof(bool));
  12.  
  13.         return tempVal;
  14.     }
  15. } // namespace util
  16. #else
  17. short util::getShort(char* buf, int from)
  18. {
  19.     buf+=from;
  20.  
  21.     bool tempVal;
  22.     memcpy(&tempVal, buf , sizeof(bool));
  23.  
  24.     return tempVal;
  25. }
  26. #endif
  27.  
  28. int main()
  29. {
  30.     char buffer[64*1024] = {};
  31.     // getShort(buffer, 20); // won't work because not using util namespace
  32.     util::getShort(buffer, 20);
  33.  
  34.     using namespace util; // can also be put at begining of source file
  35.     getShort(buffer, 20);
  36.     return 0;    
  37. }
  38.  
Try it exactly with an empty project, except change the main function to the appropriate entry point. If it doesn't work, give me the results from the console window. And if it does, do a clean on your project you are having problems with and recompile it. It could be a phase error of some sort. If that don't help, give me the console from that compile.

Oh and if you don't mind, if it works or not, give me the version of the g++ compiler by typing the compiler name in a console window (you may have to run torVars.bat from the tornado bin directory to get the proper path and environment variables setup) with the parameter --version (that is two dashes). I'd be interested in how new their compiler is.

Hope this helps.


Adrian
Feb 13 '07 #6
We started out using Tornado because it worked with 5.4 and the Motorola M68040 board. Other programmers on this project had been using Tornado with ADA on a companion system. I didn't put much effort into Tornado because I knew I would have to use Workbench when we converted to the PPC board and vxWorks 6.3. I began coding my section using Eclipse, CDT, and mingw on a PC. That was good enough for unit testing, although I couldn't CppUnit to run reliably. When we switched to workbench, management got a license for Solaris, so I am using the Exceed XTerm package to telnet to a Sun.



To answer your questions, the compiler is 3.4.4 I opened a an empty project as you suggested and made the following files:



main.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef MAIN_H_
  2.  
  3.   #define MAIN_H_
  4.  
  5.   namespace util
  6.  
  7.   {
  8.  
  9.               template< typename T >
  10.  
  11.               T getVariable(char* buf, int from);
  12.  
  13.   } // end util
  14.  
  15.   #endif // MAIN_H_
  16.  
  17.  
main.cpp
Expand|Select|Wrap|Line Numbers
  1. # include "main.h"
  2.  
  3.   #include <memory>
  4.  
  5.   namespace util
  6.  
  7.   {
  8.  
  9.  
  10.  
  11.   template< typename T >
  12.  
  13.   T getVariable(char* buf, int from)
  14.  
  15.   {
  16.  
  17.               buf+=from;
  18.  
  19.  
  20.  
  21.               T tempVal;
  22.  
  23.               memcpy(&tempVal, buf , sizeof(T));
  24.  
  25.  
  26.  
  27.               return tempVal;
  28.  
  29.   }
  30.  
  31.  
  32.  
  33.   } // namespace util  


As you can see, I made my code generic. It saves some space and looks professional.



This complied without any errors and loaded without any unresolved links the first time.



I added this code as a test:



Expand|Select|Wrap|Line Numbers
  1.  
  2.   #include <iostream>
  3.  
  4.   #include "main.h"
  5.  
  6.   using std::endl;
  7.  
  8.   using std::cout;
  9.  
  10.   using namespace util;
  11.  
  12.   void Test()
  13.  
  14.   {
  15.  
  16.               float testData = 34.68;
  17.  
  18.               char charArray[4];
  19.  
  20.               memcpy(&charArray, &testData, sizeof(float));
  21.  
  22.               cout << endl << getVariable< float >(charArray, 0) << endl;
  23.  
  24.   }
  25.  
  26.  


And it ran correctly. I went back to my original program and got more unresolved links. I fiddled around with the name space but couldn’t get it to work. Returned to the test program and got an unresolved link at load. So I am back where I started.
Feb 13 '07 #7
AdrianH
1,251 Expert 1GB
main.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef MAIN_H_
  2.  
  3.   #define MAIN_H_
  4.  
  5.   namespace util
  6.  
  7.   {
  8.  
  9.               template< typename T >
  10.  
  11.               T getVariable(char* buf, int from);
  12.  
  13.   } // end util
  14.  
  15.   #endif // MAIN_H_
  16.  
  17.  
Your problem is right here. When making a template, the template code MUST be accessable to any code trying to use it, so put the body in the header file. No source, header seperation is allowed. This is for optimisation and memory allocation for the compiler.

Hope this helps.


Adrian
Feb 13 '07 #8
Your problem is right here. When making a template, the template code MUST be accessable to any code trying to use it, so put the body in the header file. No source, header seperation is allowed. This is for optimisation and memory allocation for the compiler.

Hope this helps.


Adrian
Thanks, this will do. I can live with templates in the header file. I just wish I knew why the nongeneric code wouldn't separate. I just can't stand loose ends.
Feb 13 '07 #9
AdrianH
1,251 Expert 1GB
Thanks, this will do. I can live with templates in the header file. I just wish I knew why the nongeneric code wouldn't separate. I just can't stand loose ends.
I think you ment generic code in that statement didn't you? Anyway, you can write generic code, but it wouldn't using be a template. Trust me, I've done it in the project that I worked on when I was using VxWorks, and I did it in C. However, templates are generally faster.


Adrian
Feb 13 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: marco_segurini | last post by:
Hi, the following test program shows a solution to a problem I have had. Now, this test program is compiled and linked by VS2003 and g++ while Comeau-on-line-compiler fails with this messages:...
5
by: Jamiil | last post by:
Hey folks! I have declared an enum type inside a name space namespace jme{ enum hero_t{zorro, batmant, cat_woman, other}; class MyClass{ private: hero_t my_hero ..... }; }
8
by: Simon Brooke | last post by:
I was debugging a new XML generator tonight and trying to determine why it wasn't working; and realised my dom printer does not output XML namespace declarations. My method to output an Element...
9
by: Ivan Mascovich | last post by:
Previous posts (and compilers) confirm that class X { friend void Y() ; } ; does not match namespace
4
by: R. Nachtsturm | last post by:
Hi, Question (in short): can i somehow use the namespace tag to define that a class in its own file is actually the subclass (namespace wise) of another class? Explanation: for example, if I...
0
by: Gionni | last post by:
Hi everyone, I think I need some advice from experts. I am going to expose a web service. This web service needs to response as soon as possible, because the application that calls it just request...
1
by: toton | last post by:
Hi, I have two namespace contains class InkFrame and PrefDialog respectively. PrefDialog needs InkFrame to show the dialog over the frame. It also stores a pointer to InkFrame inside it. Now I...
9
by: vthomasset | last post by:
Hi, Sorry for the bad subject, but i couldn't figure a better one. I would like to understand why a variable declared non static in a header causes multiple symbol definitions (if included in...
17
by: Peng Yu | last post by:
Hi, I'm wondering if there is something in namespace like the 'private' keyword in class? I want to define some class or function that can only be used within that namespace. Thanks, Peng
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.