473,691 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New to c++, need help with advanced topics

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

Dec 4 '05 #1
22 2122
ma****@gmail.co m 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.

Dec 4 '05 #2
ma****@gmail.co m 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.

Dec 4 '05 #3
* ma****@gmail.co m:
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.ht ml>.

#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?
Dec 4 '05 #4
* ma****@gmail.co m:
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?
Dec 4 '05 #5

Axter wrote:
ma****@gmail.co m 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::out" instead of "using namespace
std"?

Dec 4 '05 #6
On Sun, 4 Dec 2005 03:22:54 UTC, "ma****@gmail.c om" <ma****@gmail.c om> 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
Dec 4 '05 #7
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

Dec 4 '05 #8
Ian
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
Dec 4 '05 #9
On 3 Dec 2005 19:47:39 -0800, "macawm" <ma****@gmail.c om> 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.
Dec 4 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1758
by: Jason Sirota | last post by:
I am an advanced database and vb programmer but recently my position has called for advanced archtecture descisions enterprise-wide. Although I have quite a bit of knowledge on designing relational and normalized data structures I would like to further my knowledge on large-scale distributed database design and maintenance. Can anyone suggest a decent to good book covering some of the more advanced topics of enterprise databases and...
0
1754
by: Stylus Studio | last post by:
World's Most Advanced XML Schema Editor Adds Support for IBM AlphaWorks XML Schema Quality Checker to Improve XML Schema Style and Quality BEDFORD, MA -- 09/13/2005 -- Stylus Studio (http://www.stylusstudio.com), the industry-leading provider of XML development tools for advanced data integration, today announced new support for IBM's alphaWorks XML Schema Quality Checker, furthering solidifying its position as the provider of the...
11
2123
by: Larry | last post by:
I will be teaching an eCommerce application development course using ASP.Net after many years of having taught classic ASP. (Course was interrupted by 18 months in the Middle East with my Army Reserve unit.) I need a textbook recommendation for a particular need. As I have delved into ASP.Net I find that I want to teach the course by emphasizing coding in the code behind pages but most of the books targeted at beginners to ASP use...
9
2959
by: John Robert | last post by:
Hi! Anyone has recommendations for advanced ASP.NET books? By advanced, I mean complicated stuff... not just authentification and datagrid binding I'm mainly interrested in learning more about letting my customers customize a web forms (from a database... on the fly control creation and persisting data), and other advanced topics.
3
1297
by: Luis Esteban Valencia | last post by:
Hello. I wish to buy some advanced .net or c# books.. I am interested in design patterns, windows services and more advanced topics. Thanks
7
1671
by: ashu | last post by:
i m relatively new to this group. i just want to know, is there any site which can provide any help on advanced C topics such as machine interaction through C, memory management through C etc
11
2119
by: manochavishal | last post by:
Hi, Can anyone recommend Book on C which covers all the advanced topics. Looking for the most advanced book on C. i have gone thru Expert C Programming. Something on those lines with multi threading and algorithms. Cheers Vishal
9
5346
by: Brett Romero | last post by:
I'd like to know what you have read lately on c# that could be considered advanced and that you would recommend. I'm looking for in-depth rather than reference books. No starter books and something that uses .NET 2.0 or published no later than 2004. Based on Amazon book reviews, the only thing I've found worth buying is: The Elements of C# Style (Paperback) (Jul 31, 2006)
0
2454
by: passion | last post by:
"Specialized Search Engines" along with Google Search Capability (2 in 1): http://specialized-search-engines.blogspot.com/ Billions of websites are available on the web and plenty of extremely good search engines are there like Google, Yahoo and Live to name few of them. Though this search engines have extremely efficient, complex and beautiful algorithms designed by gems of the industry, but still they may not deliver best results for...
0
8531
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,...
0
9079
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, 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...
0
8951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7623
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6457
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 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...
0
4322
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...
0
4550
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2227
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1952
bsmnconsultancy
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...

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.