Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > New to c++, need help with advanced topics

Reply
Thread Tools

New to c++, need help with advanced topics

 
 
macAWM@gmail.com
Guest
Posts: n/a
 
      12-04-2005
Hi list,
First let me explain that my background is in Java and I am quite
spoiled to its niceties (read "less ambiguous nature"). Anyway to my
problems.

1. I want to write my own library for what I consider to be some holes
in the standard language. How do I go about writing and compiling this
without this stupid error about not having a 'main' function. I don't
want a stupid 'main' function. (I'm compiling using gcc 4.0.) I would
also like to have all my functions use the std:: scope.

2. For the time being I want this library to be a two file entity, a
header file and an implementation file. Below is what I'm testing but I
can't discern the errors the compiler is throwing at me. I'm obviously
not educated on the finer details of c++ but I've got a pile of books
and this is what I gathered on my own.

<code>
Header:
template <typename T>
string toString(const T &);

cpp file:
#include <sstream>
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

template <typename T>
string toString(const T &i) {
ostringstream oss;
oss << fixed << i;
return oss.str();
}
</code>

3. Now obviously I don't need an executable file, just object code. How
do I simply compile for object code and package it?

TIA,
Anthony

 
Reply With Quote
 
 
 
 
Axter
Guest
Posts: n/a
 
      12-04-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi list,
> First let me explain that my background is in Java and I am quite
> spoiled to its niceties (read "less ambiguous nature"). Anyway to my
> problems.
>
> 1. I want to write my own library for what I consider to be some holes
> in the standard language. How do I go about writing and compiling this
> without this stupid error about not having a 'main' function. I don't
> want a stupid 'main' function. (I'm compiling using gcc 4.0.) I would
> also like to have all my functions use the std:: scope.
>
> 2. For the time being I want this library to be a two file entity, a
> header file and an implementation file. Below is what I'm testing but I
> can't discern the errors the compiler is throwing at me. I'm obviously
> not educated on the finer details of c++ but I've got a pile of books
> and this is what I gathered on my own.
>
> <code>
> Header:
> template <typename T>
> string toString(const T &);
>
> cpp file:
> #include <sstream>
> #include <string>
> #include <iomanip>
> #include <iostream>
>
> using namespace std;
>
> template <typename T>
> string toString(const T &i) {
> ostringstream oss;
> oss << fixed << i;
> return oss.str();
> }
> </code>
>
> 3. Now obviously I don't need an executable file, just object code. How
> do I simply compile for object code and package it?
>

First of all, I would be carefull how you word your question in a C++
topic area, so-as to avoid a flame war, instead of getting your
question answered.

How you create an object file depends on your compiler. I believe for
gcc the option is -o
You should be able to determine the correct option by reading your man
page for gcc.

With the code you posted, you would not be able to create a usable
object file, since you're using template code, and the above template
implementation needs to be in the header file, and not your *.cpp file.

Also, you want to avoid adding using namespace std in your header file,
because then you force all code referring to your header to be exposed
to the std namespace in the global namespace.

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-04-2005
(E-Mail Removed) wrote:
> Hi list,
> First let me explain that my background is in Java and I am quite
> spoiled to its niceties (read "less ambiguous nature"). Anyway to my
> problems.
>
> 1. I want to write my own library for what I consider to be some holes
> in the standard language. How do I go about writing and compiling this
> without this stupid error about not having a 'main' function. I don't
> want a stupid 'main' function. (I'm compiling using gcc 4.0.) I would
> also like to have all my functions use the std:: scope.


Usually C++ compilers by default will build and "link". If you want to
create a "library" you need to ask it to compile only. This is usually
done with the "-c" option (or /c option for MSVC's "cl" compiler). Once
you've created a bunch o ".o" files (or .obj files) you may want to
either create a ".so" (shared object) (or a .dll for MS cl). For this I
suggest you read up on the documentation - or perhaps you want to create
a ".a" (archive or library of object files) (or a .lib for MSVC) which I
also suggest you read the docs on the "ar" command (or the lib command).

>
> 2. For the time being I want this library to be a two file entity, a
> header file and an implementation file. Below is what I'm testing but I
> can't discern the errors the compiler is throwing at me. I'm obviously
> not educated on the finer details of c++ but I've got a pile of books
> and this is what I gathered on my own.
>
> <code>
> Header:
> template <typename T>
> string toString(const T &);
>
> cpp file:
> #include <sstream>
> #include <string>
> #include <iomanip>
> #include <iostream>
>
> using namespace std;
>
> template <typename T>
> string toString(const T &i) {
> ostringstream oss;
> oss << fixed << i;
> return oss.str();
> }
> </code>


You can only have a header file if you want this to work in gcc or you
have to use a compiler that supports template "export".

------------------- header.h ---------------------
#include <sstream>
#include <string>
#include <iomanip>
#include <iostream>

template <typename T>
std::string toString(const T &i) {
std:: ostringstream oss;
oss << std::fixed << i;
return oss.str();
}

------------------ end ---------------------------

You might want to consider putting it in a namespace and putting header
"guards" in the file.


------------------- header.h ---------------------
#ifndef x_header_h_x
#define x_header_h_x

#include <sstream>
#include <string>
#include <iomanip>
#include <iostream>

namespace macawm
{

template <typename T>
std::string toString(const T &i) {
std:: ostringstream oss;
oss << std::fixed << i;
return oss.str();
}

} // namespace macawm

#endif // x_header_h_x
------------------ end ---------------------------




>
> 3. Now obviously I don't need an executable file, just object code. How
> do I simply compile for object code and package it?


In this case you don't. All you need is the header file.

Look at other examples - Austria C++ (shameless plug) or boost, loki,
ACE etc.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-04-2005
* (E-Mail Removed):
> First let me explain that my background is in Java and I am quite
> spoiled to its niceties (read "less ambiguous nature").


Actually it's the other way around: C++ is less ambigious, more formally
well-defined, and depends much less than Java on dynamic type checking.


> Anyway to my
> problems.
>
> 1. I want to write my own library for what I consider to be some holes
> in the standard language.


You can be sure that that has already been done.

Are you familiar with the Boost library?

That's almost a standard library number 2 (parts of Boost will be part
of the standard library in the next standard), and if you do anything in
C++ you should have it -- and use it: <url: http://www.boost.org>.


> How do I go about writing and compiling this
> without this stupid error about not having a 'main' function. I don't
> want a stupid 'main' function. (I'm compiling using gcc 4.0.)


With most compilers it's option "-c".


> I would also like to have all my functions use the std:: scope.


Formally you can't. In practice the compiler won't stop you, but it's
not a good idea. The menu of what you're allowed to put in namespace
std (it's a namespace, not a scope) is very limited, essentially only
specializations of existing std namespace templates.


> 2. For the time being I want this library to be a two file entity, a
> header file and an implementation file. Below is what I'm testing but I
> can't discern the errors the compiler is throwing at me. I'm obviously
> not educated on the finer details of c++ but I've got a pile of books
> and this is what I gathered on my own.
>
> <code>
> Header:
> template <typename T>
> string toString(const T &);


That's boost::lexical_cast.

Anyway, in your header file you need to include <string>.


> cpp file:


Here you should #include your header file first of all.

See chapter 1.6 in
<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html>.


> #include <sstream>
> #include <string>
> #include <iomanip>
> #include <iostream>
>
> using namespace std;
>
> template <typename T>
> string toString(const T &i) {
> ostringstream oss;
> oss << fixed << i;
> return oss.str();
> }
> </code>
>
> 3. Now obviously I don't need an executable file, just object code. How
> do I simply compile for object code and package it?


See above, plus your compiler's documentation.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
macawm
Guest
Posts: n/a
 
      12-04-2005

Axter wrote:
> (E-Mail Removed) wrote:
> > Hi list,
> > First let me explain that my background is in Java and I am quite
> > spoiled to its niceties (read "less ambiguous nature"). Anyway to my
> > problems.
> >
> > 1. I want to write my own library for what I consider to be some holes
> > in the standard language. How do I go about writing and compiling this
> > without this stupid error about not having a 'main' function. I don't
> > want a stupid 'main' function. (I'm compiling using gcc 4.0.) I would
> > also like to have all my functions use the std:: scope.
> >
> > 2. For the time being I want this library to be a two file entity, a
> > header file and an implementation file. Below is what I'm testing but I
> > can't discern the errors the compiler is throwing at me. I'm obviously
> > not educated on the finer details of c++ but I've got a pile of books
> > and this is what I gathered on my own.
> >
> > <code>
> > Header:
> > template <typename T>
> > string toString(const T &);
> >
> > cpp file:
> > #include <sstream>
> > #include <string>
> > #include <iomanip>
> > #include <iostream>
> >
> > using namespace std;
> >
> > template <typename T>
> > string toString(const T &i) {
> > ostringstream oss;
> > oss << fixed << i;
> > return oss.str();
> > }
> > </code>
> >
> > 3. Now obviously I don't need an executable file, just object code. How
> > do I simply compile for object code and package it?
> >

> First of all, I would be carefull how you word your question in a C++
> topic area, so-as to avoid a flame war, instead of getting your
> question answered.


Point taken.

> How you create an object file depends on your compiler. I believe for
> gcc the option is -o
> You should be able to determine the correct option by reading your man
> page for gcc.


Believe me I've tried reading the man pages, but they're awful long and
more complex than I need.

> With the code you posted, you would not be able to create a usable
> object file, since you're using template code, and the above template
> implementation needs to be in the header file, and not your *.cpp file.


So, I need to put the actual implementation code in the header file?!?
How will I compile it then? Last I checked a header file is used by the
preprocessor not in compiler itself.

> Also, you want to avoid adding using namespace std in your header file,
> because then you force all code referring to your header to be exposed
> to the std namespace in the global namespace.


More along the lines of "using std:ut" instead of "using namespace
std"?

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-04-2005
* (E-Mail Removed):
> cpp file:
>
> template <typename T>
> string toString(const T &i) {
> ostringstream oss;
> oss << fixed << i;
> return oss.str();
> }


Sorry, didn't see that when I posted my first reply.

Template code can in practice not be separately compiled: such code
meant to be reused must, in practice, be placed in header files.

The way you're doing it you can only provide specializations for a fixed
number of types (and you're not doing even that).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
David
Guest
Posts: n/a
 
      12-04-2005
On Sun, 4 Dec 2005 03:22:54 UTC, "(E-Mail Removed)" <(E-Mail Removed)> wrote:

> Hi list,
> First let me explain that my background is in Java and I am quite
> spoiled to its niceties (read "less ambiguous nature"). Anyway to my
> problems.
>
> 1. I want to write my own library for what I consider to be some holes
> in the standard language. How do I go about writing and compiling this
> without this stupid error about not having a 'main' function. I don't
> want a stupid 'main' function. (I'm compiling using gcc 4.0.) I would
> also like to have all my functions use the std:: scope.


You may compile a C/C++ file into an object file. A group of object
files is usually called a library. Read the help for your compiler
to find the options related to "compile only". These are the options
you will use to create object files. The compiler or perhaps a library
manager can be used to combine and manage a group of object files to
create a library. The linker is used to combine objects and libraries
into executable and dynamic link libraries. The compiler/librarian/linker
may be presented as a single interface or multiple interfaces depending on
your tool set. In the case of gcc, you will want to become familiar
with its options.

You are free to insert the scope directives you prefer in your header
files.

> 2. For the time being I want this library to be a two file entity, a
> header file and an implementation file. Below is what I'm testing but I
> can't discern the errors the compiler is throwing at me. I'm obviously
> not educated on the finer details of c++ but I've got a pile of books
> and this is what I gathered on my own.
>
> <code>
> Header:
> template <typename T>
> string toString(const T &);
>
> cpp file:
> #include <sstream>
> #include <string>
> #include <iomanip>
> #include <iostream>
>
> using namespace std;
>
> template <typename T>
> string toString(const T &i) {
> ostringstream oss;
> oss << fixed << i;
> return oss.str();
> }
> </code>


Header files (.h, .hh .hpp) generally carry the definitions and templates.
Code files (.c .cc .cpp) generally carry the implementations. Templates
are generally in the header files and compiled each time they are actually
used. An uninstantiated/unused template does not have any code component
that would exist in an object/library file.

> 3. Now obviously I don't need an executable file, just object code. How
> do I simply compile for object code and package it?


You can check the man gcc and gcc -h commands. I believe you are looking
for the gcc -c option. I'm working from memory and won't bother to look it
up for you. See my answer to #1 for a more complete description.

> TIA,
> Anthony


David
 
Reply With Quote
 
macawm
Guest
Posts: n/a
 
      12-04-2005
Ok, I believe I have a better grasp of what I need to do. Many thanks
for the (shameless) plugs to Boost and other libraries that already do
what I need. I really don't feel like making any new wheels.

G.M. I really like the attention to detail i.e. namespace macawm { ...
}. My current literature doesn't mention this explicitly. And I have
definitely put the guards in, I simply removed them for the post.

> Actually it's the other way around: C++ is less ambigious, more formally
> well-defined, and depends much less than Java on dynamic type checking.

A.S. I agree with this statement, but _I feel_ that dynamic type
checking is a must have. (Please no language debate.)

Thanks for the insightful and precise info.
Anthony

 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      12-04-2005
macawm wrote:
>
>>Actually it's the other way around: C++ is less ambigious, more formally
>>well-defined, and depends much less than Java on dynamic type checking.

>
> A.S. I agree with this statement, but _I feel_ that dynamic type
> checking is a must have. (Please no language debate.)
>

dynamic_cast ?

Ian
 
Reply With Quote
 
W Marsh
Guest
Posts: n/a
 
      12-04-2005
On 3 Dec 2005 19:47:39 -0800, "macawm" <(E-Mail Removed)> wrote:


>So, I need to put the actual implementation code in the header file?!?
>How will I compile it then? Last I checked a header file is used by the
>preprocessor not in compiler itself.


Then you've never learned about header files properly.

When you #include a header file, the preprocessor simply inserts the
file in that location. Copying and pasting the header file directly
into your source code would achieve the same thing (but that would be
a nightmare!).

Header files are usually used for declarations. Sometimes you'll
define a function or class method inside of a header file, but the one
thing you'll (probably) never do is declare an instance of anything
(as this will end up in multiple symbol errors when you link should
you use the header file more than once).

So if you're writing a template, you'll be writing code inside of the
header file. This is because the source file using the template will
need its definition to insert the necessary code. It can't do this
with compiled code from another module.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Advanced Python Programming Oxford Lectures [was: Re: *Advanced*Python book?] Michele Simionato Python 1 03-27-2010 06:10 AM
is there any site which can provide any help on advanced C topics ashu C Programming 7 01-26-2006 01:19 PM
Advanced topics... optimising / C++ / OOP / RefCounting / type-safety st_ev_fe@hotmail.com C++ 4 11-15-2005 12:35 PM
Need help on topics to cover David Computer Information 0 07-31-2004 01:11 PM
Need Remote Button with sub-topics news.frontiernet.net Javascript 2 09-16-2003 03:42 AM



Advertisments