need help in maintaining a project

R

Rex_chaos

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!
 
M

Mike Wahler

Rex_chaos said:
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
 
R

Rex_chaos

Mike Wahler said:
/* =============================== */
/* 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.
 
R

Rex_chaos

Mike Wahler said:
/* =============================== */
/* 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<typename ADT>
class A
{...
class inA
{
}
}

The more annoying thing is that I have an inner class defined inside
class A.
 
M

Mike Wahler

Rex_chaos said:
"Mike Wahler" <[email protected]> wrote in message

Thanks a lot. But I still have a question. Take your example, but in
my case, class A is a template class

template<typename 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
 
R

Rex_chaos

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(string 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::string)'
 
M

Mike Wahler

Rex_chaos said:
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(string 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::string)'

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top