Hi all,
I am writing a project in C++. I am going to compile all my source
to a library(.a). So I can only distribute the lib as well as the
header file to my client. However, I don't quite familiar with C++
programming. In my project, I have defined a heap of classes, the
definition of which defined in a hearder file. On the other hand, I
would like to limit all the classes in a namespace.
Can anyone please tell me how to write the header file? Please give me
an simple example.
Any reply will be highly appreciated! 6 1381
"Rex_chaos" <re*******@21cn .com> wrote in message
news:f7******** *************** **@posting.goog le.com... Hi all, I am writing a project in C++. I am going to compile all my source to a library(.a). So I can only distribute the lib as well as the header file to my client. However, I don't quite familiar with C++ programming. In my project, I have defined a heap of classes, the definition of which defined in a hearder file. On the other hand, I would like to limit all the classes in a namespace.
Can anyone please tell me how to write the header file? Please give me an simple example.
/* =============== =============== = */
/* myheader.h */
namespace MyStuff
{
class A { /* etc */ };
class B { /* etc */ };
}
/* =============== =============== = */
If you implement any member functions or static data
items in a separate '.cpp' file:
/* =============== =============== = */
/* myimp.cpp */
#include "myheader.h "
namespace MyStuff
{
void A::foo() { /* etc */ }
int B::whatsit = 42;
}
/* =============== =============== = */
Code that uses the classes:
/* =============== =============== = */
/* module.cpp */
#include "myheader.h "
int main()
{
using MyStuff::A;
using MyStuff::B;
/* or perhaps simply:
using namespace MyStuff;
*/
A thingy;
thingy.foo();
/* etc */
return 0;
}
/* =============== =============== = */
HTH,
-Mike
"Mike Wahler" <mk******@mkwah ler.net> wrote in message news:<Wu******* **********@news read3.news.pas. earthlink.net>. .. "Rex_chaos" <re*******@21cn .com> wrote in message news:f7******** *************** **@posting.goog le.com... Hi all, I am writing a project in C++. I am going to compile all my source to a library(.a). So I can only distribute the lib as well as the header file to my client. However, I don't quite familiar with C++ programming. In my project, I have defined a heap of classes, the definition of which defined in a hearder file. On the other hand, I would like to limit all the classes in a namespace.
Can anyone please tell me how to write the header file? Please give me an simple example.
/* =============== =============== = */ /* myheader.h */ namespace MyStuff { class A { /* etc */ }; class B { /* etc */ }; } /* =============== =============== = */
If you implement any member functions or static data items in a separate '.cpp' file:
/* =============== =============== = */ /* myimp.cpp */
#include "myheader.h "
namespace MyStuff { void A::foo() { /* etc */ } int B::whatsit = 42; } /* =============== =============== = */
Code that uses the classes:
/* =============== =============== = */ /* module.cpp */
#include "myheader.h "
int main() { using MyStuff::A; using MyStuff::B;
/* or perhaps simply: using namespace MyStuff; */ A thingy; thingy.foo(); /* etc */ return 0; } /* =============== =============== = */
HTH, -Mike
Thanks a lot. It do work.
"Mike Wahler" <mk******@mkwah ler.net> wrote in message news:<Wu******* **********@news read3.news.pas. earthlink.net>. .. "Rex_chaos" <re*******@21cn .com> wrote in message news:f7******** *************** **@posting.goog le.com... Hi all, I am writing a project in C++. I am going to compile all my source to a library(.a). So I can only distribute the lib as well as the header file to my client. However, I don't quite familiar with C++ programming. In my project, I have defined a heap of classes, the definition of which defined in a hearder file. On the other hand, I would like to limit all the classes in a namespace.
Can anyone please tell me how to write the header file? Please give me an simple example.
/* =============== =============== = */ /* myheader.h */ namespace MyStuff { class A { /* etc */ }; class B { /* etc */ }; } /* =============== =============== = */
If you implement any member functions or static data items in a separate '.cpp' file:
/* =============== =============== = */ /* myimp.cpp */
#include "myheader.h "
namespace MyStuff { void A::foo() { /* etc */ } int B::whatsit = 42; } /* =============== =============== = */
Code that uses the classes:
/* =============== =============== = */ /* module.cpp */
#include "myheader.h "
int main() { using MyStuff::A; using MyStuff::B;
/* or perhaps simply: using namespace MyStuff; */ A thingy; thingy.foo(); /* etc */ return 0; } /* =============== =============== = */
HTH, -Mike
Thanks a lot. But I still have a question. Take your example, but in
my case, class A is a template class
template<typena me ADT>
class A
{...
class inA
{
}
}
The more annoying thing is that I have an inner class defined inside
class A.
"Rex_chaos" <re*******@21cn .com> wrote in message
news:f7******** *************** **@posting.goog le.com... "Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:<Wu******* **********@news read3.news.pas. earthlink.net>. .. "Rex_chaos" <re*******@21cn .com> wrote in message news:f7******** *************** **@posting.goog le.com... Hi all, I am writing a project in C++. I am going to compile all my source to a library(.a). So I can only distribute the lib as well as the header file to my client. However, I don't quite familiar with C++ programming. In my project, I have defined a heap of classes, the definition of which defined in a hearder file. On the other hand, I would like to limit all the classes in a namespace.
Can anyone please tell me how to write the header file? Please give me an simple example.
/* =============== =============== = */ /* myheader.h */ namespace MyStuff { class A { /* etc */ }; class B { /* etc */ }; } /* =============== =============== = */
If you implement any member functions or static data items in a separate '.cpp' file:
/* =============== =============== = */ /* myimp.cpp */
#include "myheader.h "
namespace MyStuff { void A::foo() { /* etc */ } int B::whatsit = 42; } /* =============== =============== = */
Code that uses the classes:
/* =============== =============== = */ /* module.cpp */
#include "myheader.h "
int main() { using MyStuff::A; using MyStuff::B;
/* or perhaps simply: using namespace MyStuff; */ A thingy; thingy.foo(); /* etc */ return 0; } /* =============== =============== = */
HTH, -Mike
Thanks a lot. But I still have a question. Take your example, but in my case, class A is a template class
template<typena me ADT> class A {... class inA { } }
}; The more annoying thing is that I have an inner class defined inside class A.
So what's the problem? Do you have code that won't
compile? That doesn't do what you expect?
Post the exact code giving you trouble, and we
can have a look.
-Mike
I modify the code as
/* =============== =============== = */
/* myheader.h */
namespace MyStuff
{
template <typename ADT>
class A
{ ...
A(string str);
...
};
class B { /* etc */ };
}
/* =============== =============== = */
/* myimp.cpp */
#include "myheader.h "
namespace MyStuff
{
template <typename ADT>
A<ADT>::A(strin g str) { /* etc */ }
/ * Code that uses the classes */
/* =============== =============== = */
/* module.cpp */
#include "myheader.h "
using namespace MyStuff;
int main()
{
A<int> A("hello");
return 0;
}
However, it can't pass the compilation and reports that
[Linker error] undefined reference to `MyStuff::A<int >::A(std::strin g)'
"Rex_chaos" <re*******@21cn .com> wrote in message
news:f7******** *************** **@posting.goog le.com... I modify the code as
/* =============== =============== = */ /* myheader.h */ namespace MyStuff { template <typename ADT> class A { ...
Don't put stuff like that in the code you post.
I have to take it out to compile it.
A(string str);
If you use a standard library type, #include the needed header
...
And again, I have to edit.
};
class B { /* etc */ }; }
/* =============== =============== = */ /* myimp.cpp */
#include "myheader.h "
namespace MyStuff { template <typename ADT> A<ADT>::A(strin g str) { /* etc */ }
You can't do this. Unless your compiler supports
'export' (afaik, only Comeau does), you must
put the full definition of a class template
in the header. / * Code that uses the classes */ /* =============== =============== = */ /* module.cpp */
#include "myheader.h "
using namespace MyStuff;
int main() { A<int> A("hello");
return 0; }
However, it can't pass the compilation and reports that
[Linker error] undefined reference to `MyStuff::A<int >::A(std::strin g)'
That's not a compiler error, it's a linker error.
Not the same thing.
/* =============== =============== = */
/* myheader.h */
#include <iostream>
#include <string>
namespace MyStuff
{
template <typename ADT>
class A
{
public:
// full ctor definition:
A(const std::string& str)
{
std::cout << str << '\n';
}
};
class B { /* etc */ };
}
/* =============== =============== = */
/* myimp.cpp */
/* nothing left to to */
/* =============== =============== = */
/* module.cpp */
#include "myheader.h "
using namespace MyStuff;
int main()
{
A<int> A("hello");
return 0;
}
-Mike This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chad Z. Hower aka Kudzu |
last post by:
A few of you may recognize me from the recent posts I have made about Indy
<http://www.indyproject.org/indy.html>
Those of you coming to .net from the Delphi world know truly how unique and
"huge" Indy is both as a project, in support, development, and use. But
Indy is new to the .net world.
Indy is a HUGE library implementing over 120 internet protocols and
standards and comes with complete source. Its an open source project, but
not...
|
by: yatharth |
last post by:
Hi,
Its Urget plz help me out on this issue.
I m working on a project in which we have used both java and .net,
and the issue is how to maintain security ,if we move from java page
link to .net page and back .We have used the .net authentication
security in our .net project and suppose the user come from java
page(JSP or any java applet) by clicking the link ,how i will
autheticate him so that he/she can use my .net page after proper
|
by: Charles Law |
last post by:
I am faced with an enormous task: inserting, updating and generally
maintaining code header blocks for a project. Each code file/module requires
a header, and each major function requires a different style of header. The
comments I will have to enter myself.
Does anyone know of a tool that automates this process? Ideally, the tool
would allow standard text to appear in all blocks, which could be changed
once and then would automatically...
|
by: HK |
last post by:
With VB.NET 2005, and a Windows Form, running on a dual CPU box, I need to
take a recordset (e.g. 100,000 records) and spawn a thread to handle an
internet XML transaction routine for each of the records. This is a nice
use of threading because those internet requests are going against 3rd party
servers that often have 1 second latency problems and so handling them with
multiple threads is the fastest way to get through all the records in...
|
by: Raymond |
last post by:
They say it's easier, but has anyone tried
maintaining an ASP.NET site without the
source code of the dlls? This was not a problem
with classic ASP, all the code was almost always
just in text files, easily viewable and most importantly
readily AVAILABLE on the site, to anyone access to
the site's folder.
But just imagine, bunch of companies out there, managed
by non-technical people, who have had a bunch of outside
| |
by: aswin.paranji |
last post by:
Hi All,
Currently I'm working on a migration project from asp to asp.net. I use
a tool asp2aspx to migrate the asp pages. the problem is when i migrate
to asp.net 2.0 i'm getting lots of compilation error. when i migrate to
asp.net 1.1 i dont have any errors. so i have to stick with asp.net 1.1
There is an other site which runs on asp.net 2.0.
Now my login page is in the migrated project which is running the
asp.net 1.1
|
by: Mahathi |
last post by:
Hi
I have a small problem in maintaining the state of a check box. Please do me a favour by telling me the procedure how to do that.
My requirement is that
"I have to map some roles with that of the users of the project. I have used checkboxes for selecting the roles that a particular user has.
For example, an adminstrator has all roles in an organisation. Similarly an Employee has limited roles.
Here let us take administrator...
|
by: =?Utf-8?B?RGVyZWs=?= |
last post by:
Does anyone know if there is an idiots guide to maintaining Active Directory
using VB.NET.
I have found bits and pieces about ldap_add, ldap_modify etc on the web but
I could do with more hand holding.
--
Derek
|
by: KW |
last post by:
I have an application that runs in any of Access 2000 thru Access 2007
to accommodate my customers' various environments. I implemented code
to call a function that uses the LoadCustomUI method to load in my
custom ribbon for Access 2007. This function is called only when the
check for running Access version 12 or higher is true.
Everything works like I want in Access 2007. The problem is when I
compile it on Access 2000,2002,2003,...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |