c ++新手,需要有关高级主题的帮助 [英] New to c++, need help with advanced topics

查看:55
本文介绍了c ++新手,需要有关高级主题的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨列表,

首先让我解释一下,我的背景是用Java编写的,而且我很贬低它的细节(阅读不那么模糊的性质)。无论如何我的

问题。


1.我想写我自己的库,我认为是一些漏洞

用标准语言。如何编写和编译这个

而没有关于没有''main''函数的愚蠢错误。我不希望
想要一个愚蠢的主要功能。 (我正在使用gcc 4.0编译。)我会

也喜欢让我的所有函数都使用std :: scope。


2.对于我希望这个库成为一个双文件实体,一个

头文件和一个实现文件。下面是我正在测试但是我没有看到编译器向我抛出的错误。
无法辨别。我很明显没有接受过关于c ++精细细节的教育,但我有一堆书

这就是我自己收集的。


< code>

标题:

模板< typename T>

string toString( const T&);


cpp文件:

#include< sstream>

#include< string>

#include< iomanip>

#include< iostream>


使用命名空间std;


模板< typename T>

string toString(const T& i){

ostringstream oss;

oss< ;<固定<< i;

返回oss.str();

}

< / code>


3.现在显然我不需要可执行文件,只需要对象代码。怎么

我只是编译目标代码并打包它?


TIA,

Anthony

解决方案

ma****@gmail.com

嗨列表,
首先让我解释一下,我的背景是用Java编写的,而且我很害怕它的细节(阅读不那么模糊的性质)。无论如何我的
问题。

1.我想用自己的库来编写我认为是标准语言的漏洞。如何在没有关于没有主要功能的愚蠢错误的情况下编写和编译这个
。我不想要一个愚蠢的主要功能。 (我正在使用gcc 4.0进行编译。)我也希望我的所有函数都使用std :: scope。

2.目前我想要这个库来是一个双文件实体,一个
头文件和一个实现文件。下面是我正在测试但我无法辨别编译器向我投掷的错误。我显然没有受过关于c ++精细细节的教育,但我有一堆书
这就是我自己收集的。

< ;代码>
标题:
模板< typename T>
字符串toString(const T&);

cpp文件:
#include< ; sstream>
#include< string>
#include< iomanip>
#include< iostream>

使用命名空间std;

模板< typename T>
字符串toString(const T& i){
ostringstream oss;
oss<<固定<< i;
返回oss.str();
}
< / code>

3.现在显然我不需要可执行文件,只需目标代码。我如何简单地编译目标代码并将其打包?



首先,我会小心你如何用C ++来表达你的问题

主题区域,以避免火焰战争,而不是让你的

问题得到解答。


如何创建目标文件取决于你的编译器。我相信

gcc选项是-o

你应该能够通过阅读你的男人确定正确的选项

页面为gcc。


使用您发布的代码,您将无法创建可用的

目标文件,因为您正在使用模板代码,以上模板

实现需要在头文件中,而不是你的* .cpp文件。


另外,你想避免在你的命名空间中使用命名空间std头文件,

,因为那时你强制所有引用你头的代码暴露给全局命名空间中的std命名空间。


ma****@gmail.com 写道:

嗨列表,
首先让我解释一下,我的背景是用Java编写的,而且我很害怕它的细节(阅读不那么模糊的性质)。无论如何我的
问题。

1.我想用自己的库来编写我认为是标准语言的漏洞。如何在没有关于没有主要功能的愚蠢错误的情况下编写和编译这个
。我不想要一个愚蠢的主要功能。 (我正在使用gcc 4.0进行编译。)我也希望我的所有函数都使用std :: scope。


通常,C ++编译器默认会构建和链接。如果你想

创建一个库你需要要求它只编译。通常使用-c完成

选项(或MSVC的cl编译器的/ c选项)。一旦

你创造了一堆o.o您可能想要的文件(或.obj文件)
要么创建一个.so,要么创建一个.so文件。 (共享对象)(或MS cl的.dll)。为此,我建议您阅读文档 - 或者您想要创建

a.a (目标文件的存档或库)(或MSVC的.lib),我建议你阅读ar的文档。命令(或lib命令)。

2.目前我希望这个库是一个双文件实体,一个
头文件和一个实现文件。下面是我正在测试但我无法辨别编译器向我投掷的错误。我显然没有受过关于c ++精细细节的教育,但我有一堆书
这就是我自己收集的。

< ;代码>
标题:
模板< typename T>
字符串toString(const T&);

cpp文件:
#include< ; sstream>
#include< string>
#include< iomanip>
#include< iostream>

使用命名空间std;

模板< typename T>
字符串toString(const T& i){
ostringstream oss;
oss<<固定<< i;
返回oss.str();
}
< / code>


如果你想让它在gcc中工作,你只能有一个头文件或者你需要使用支持模板export的编译器.b $ b。


------------------- header.h ---------------- -----

#include< sstream>

#include< string>

#include< iomanip>

#include< iostream>


模板< typename T>

std :: string toString(const T& i){

std :: ostringstream oss;

oss<< std :: fixed<<我;

返回oss.str();

}


------------ ------结束---------------------------


您可能想要考虑将其放入命名空间并放置标题

" guards"在文件中。

------------------- header.h ---------------- -----

#ifndef x_header_h_x

#define x_header_h_x


#include< sstream>

#include< string>

#include< iomanip>

#include< iostream>


名称空间macawm

{


模板< typename T>

std :: string toString(const T& i){

std :: ostringstream oss;

oss<< std :: fixed<< i;

返回oss.str();

}


} //命名空间macawm

#endif // x_header_h_x

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

3.现在显然我不需要可执行文件,只需要对象代码。我如何简单地编译目标代码并打包它?




在这种情况下你不会。所有你需要的是头文件。


看看其他例子 - 奥地利C ++(无耻插件)或者提升,loki,

ACE等。


* mavis@gmail.com :< blockquote class =post_quotes>首先让我解释一下,我的背景是用Java编写的,而且我很害怕它的细节(阅读不那么模糊的性质)。


实际上它是相反的:C ++不那么暧昧,更正式地定义,并且在动态类型上远远低于Java检查。


无论如何我的
问题。

1.我想写我自己的库,我认为是一些漏洞/>标准语言。


你可以肯定已经完成了。


你熟悉Boost图书馆吗?

这几乎是标准的2号库(Boost的部分将是下一个标准中标准库的
部分),如果你在
中做任何事情
C ++你应该拥有它 - 并使用它:< url:http://www.boost.org>。


如何编写和编译没有这个关于没有主要功能的愚蠢错误。我不想要一个愚蠢的主要功能。 (我正在使用gcc 4.0进行编译。)


对于大多数编译器,它的选项是-c。


我我还希望我的所有函数都使用std :: scope。


正式你不能。在实践中,编译器不会阻止你,但它不是一个好主意。允许放入命名空间的菜单

std(它是命名空间,而不是范围)是非常有限的,基本上只有

现有的专业std命名空间模板。


2.目前我希望这个库是一个双文件实体,一个
头文件和一个实现文件。下面是我正在测试但我无法辨别编译器向我投掷的错误。我显然没有受过关于c ++精细细节的教育,但我有一堆书
这就是我自己收集的。

< ;代码>
标题:
模板< typename T>
字符串toString(const T&);


那是'boost :: lexical_cast。


无论如何,在你的头文件中你需要包含< string>。 />

cpp文件:


首先你要#include你的头文件。


见第1.6章

< url:http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html>。


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

使用命名空间std;

模板< typename T>
字符串toString(const T& i){
ostringstream oss;
oss<<固定<< i;
返回oss.str();
}
< / code>

3.现在显然我不需要可执行文件,只需目标代码。我如何简单地编译目标代码并将其打包?




见上文,以及编译器的文档。

-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


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

解决方案

ma****@gmail.com 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.


ma****@gmail.com 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.


* ma****@gmail.com:

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?


这篇关于c ++新手,需要有关高级主题的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆