使用功能模板的问题 [英] Problems using function templates

查看:73
本文介绍了使用功能模板的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试编写一个函数模板,

可以用随机的东西填充任意类型的变量,

但我似乎无法让我的功能模板工作。


在我的.h文件中,我已经宣布了这样的功能模板,

在我正在构建的类中:


模板< typename T> T RandBits();


在我的.cpp文件中我实现了这样:


/ *功能:RandBits * /

模板< typename T> T hdd :: HexGen :: RandBits(){

T bits = T();

for(int i = 0; i< sizeof(T); i ++ ){b / b
位<< = 8; //左移8

bits | = rand()%0xFF; //插入8''随机''位

// printf("%08X \ n",bits); //显示进度

}

返回位;

} // EndOf RandBits()


虽然常见问题解答说我应该使用''extern'',但我已经省略了

它,因为我收到了一条错误信息。


我没有收到任何链接错误,但是根据常见问题解答的建议,

我已将以下内容包含在同一个.cpp文件中:


模板hdd :: HexGen :: RandBits< int>();


现在,在另一个.cpp文件中,我已经掌握了我的主要内容,我是

instansiate我的班级的一个对象,并调用模板

这样的函数:


long lngRand = hg.RandBits< long>( );

cout<< " \\\
" << hex<< lngRand<< endl;


''hg''当然是上述类的一个对象。


使用VS VC 6.0 SP5(?),我收到以下错误:


c:\programfiler \ microsoft visual

studio\myprojects\tutorialtests\myxor\main.cpp( 12):错误C2062:输入

''long''意外


为什么长意外?谁能看到我做错了什么?我想

我现在已经盯着这个问题,所以所有的输入都是

appreaciated :-)


-

Fred H


void FredH ::联络(){

TextToSpeach.say(" frode at age dee dee dot en oh;

}

解决方案



Fred H写道:


在我的.cpp文件中,我已经实现了这样:
[...]
即使常见问题解答说我应该使用''extern '',我省略了它,因为我收到了使用它的错误信息。




如果你把实现放在一个cpp文件中用它生成代码时,编译器找不到

。如果''extern''不起作用(我实际上是/ b $ b naver使用它)将实现放在

头文件的底部或(就像我一样)在hexgen.inl文件中放置一个

#include" hexgen.inl"

在标题的底部。


另一个问题是VC编译器(至少6.0,7.0和7.1)支持
不支持explicite模板规范所以你可能需要

重写你的函数类似


模板< typename T> T hdd :: HexGen :: RandBits(T& BitsRef)

{

[...]

}


我认为模板必须在头文件中定义,而不是在.cpp

文件中定义。


Fred H < SE **** @ nospam.com>在留言中写道

news:op ************** @ news.mimer.no ...


我我目前正在尝试编写一个功能模板,
可以用随机的东西填充任意类型的变量,
但我似乎无法让我的功能模板工作。 />
在我的.h文件中,我已经在我正在构建的类中声明了这样的函数模板,

模板< typename T> T RandBits();

在我的.cpp文件中,我已经实现了这样:

/ *功能:RandBits * /
模板< typename T> T hdd :: HexGen :: RandBits(){
T bits = T();
for(int i = 0; i< sizeof(T); i ++){
bits< ;< = 8; //左移8
位| = rand()%0xFF; //插入8''随机'位
// printf("%08X \ nn),位; //显示进度
}
返回位;
} EndOf RandBits()

即使FAQ说我应该使用''extern'',我已经省略了它,因为我收到了使用它的错误信息。

我没有收到任何链接错误,但正如常见问题解答所建议的那样,
我在同一个.cpp文件中包含以下内容:

模板hdd :: HexGen :: RandBits< int>();

现在,在另一个.cpp文件中,我已经掌握了我的主要内容,我实现了我班级的一个对象,并将模板称为
这样的功能:

long lngRand = hg.RandBits< long> ;();
cout<< " \\\
" << hex<< lngRand<< endl;

''hg''当然是上述类的一个对象。

使用VS VC 6.0 SP5(?),我收到以下错误:

c:\programfiler \ microsoft visual
studio\myprojects\tutorialtests\myxor\main.cpp(12):错误C2062:输入
''long' '意外

为什么长意外?谁能看到我做错了什么?我想我现在已经盯着自己看这个问题了,所以所有的输入都是适用的:-)

-
Fred H
void FredH ::联系方式(){
TextToSpeach.say(frode at age dee dee dot en oh);
}



template hdd :: HexGen :: RandBits< int>();




这句话显然缺乏返回声明。

当我插入一个时,我实际收到更多错误消息:


c:\programfiler \ microsoft visual

studio\myprojects\tutorialtests\myxor\hexgen.cpp(3 0):致命错误C1001:

内部编译器错误

(编译器文件'' msc1.cpp'',第1794行)


c:\programfiler \ microsoft visual

studio\myprojects\tutorialtests\myxor\main .cpp(12):错误C2062:输入

''long''意外

执行cl.exe时出错。



I''m currently trying to write a function template that
can fill a variable of arbitrary type with ''random'' stuff,
but I can''t seem to get my function template working.

In my .h file I''ve declared the function template like this,
inside a class I''m building:

template <typename T> T RandBits();

In my .cpp file I''ve implemented it like this:

/* Function: RandBits */
template <typename T> T hdd::HexGen::RandBits() {
T bits = T();
for(int i = 0; i < sizeof(T); i++) {
bits <<= 8; //Left shift by 8
bits |= rand() % 0xFF; //Insert 8 ''random'' bits
//printf("%08X\n",bits); //Shows the progression
}
return bits;
}//EndOf RandBits()

Even though the FAQ says I should use ''extern'', I''ve omitted
it, because I get an error message using it.

I don''t get any linking errors, but as suggested by the FAQ,
I''ve included the following in the same .cpp file:

template hdd::HexGen::RandBits<int>();

Now, in another .cpp file, where I''ve got my main, I
instansiate an object of my class, and call the template
function like this:

long lngRand = hg.RandBits<long>();
cout << "\n" << hex << lngRand << endl;

''hg'' is of course a object of the mentioned class.

Using VS VC 6.0 SP5(?), I get the following error:

c:\programfiler\microsoft visual
studio\myprojects\tutorialtests\myxor\main.cpp(12) : error C2062: type
''long'' unexpected

Why is ''long'' unexpected? Can anyone see what I''m doing wrong? I suppose
I''ve stared myself blind on this problem by now, so all inputs are
appreaciated :-)

--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}

解决方案


Fred H wrote:


In my .cpp file I''ve implemented it like this: [...]
Even though the FAQ says I should use ''extern'', I''ve omitted
it, because I get an error message using it.



If you place the implementation in a cpp-file the compiler can''t find it
when generating code with it. If the ''extern'' doesn''t work (I actually
naver used it) place the implementation on the bottom of your
header-file or (as I prefer) in a hexgen.inl file and place an
#include "hexgen.inl"
at the bottom of your header.

Another problem is that the VC compiler (at least 6.0, 7.0 and 7.1) do
not support explicite template specification so you might have to
rewrite your function to something like

template <typename T> T hdd::HexGen::RandBits(T& BitsRef)
{
[...]
}


I believe templates must be defined within the header files, not in .cpp
files.

"Fred H" <se****@nospam.com> wrote in message
news:op**************@news.mimer.no...


I''m currently trying to write a function template that
can fill a variable of arbitrary type with ''random'' stuff,
but I can''t seem to get my function template working.

In my .h file I''ve declared the function template like this,
inside a class I''m building:

template <typename T> T RandBits();

In my .cpp file I''ve implemented it like this:

/* Function: RandBits */
template <typename T> T hdd::HexGen::RandBits() {
T bits = T();
for(int i = 0; i < sizeof(T); i++) {
bits <<= 8; //Left shift by 8
bits |= rand() % 0xFF; //Insert 8 ''random'' bits
//printf("%08X\n",bits); //Shows the progression
}
return bits;
}//EndOf RandBits()

Even though the FAQ says I should use ''extern'', I''ve omitted
it, because I get an error message using it.

I don''t get any linking errors, but as suggested by the FAQ,
I''ve included the following in the same .cpp file:

template hdd::HexGen::RandBits<int>();

Now, in another .cpp file, where I''ve got my main, I
instansiate an object of my class, and call the template
function like this:

long lngRand = hg.RandBits<long>();
cout << "\n" << hex << lngRand << endl;

''hg'' is of course a object of the mentioned class.

Using VS VC 6.0 SP5(?), I get the following error:

c:\programfiler\microsoft visual
studio\myprojects\tutorialtests\myxor\main.cpp(12) : error C2062: type
''long'' unexpected

Why is ''long'' unexpected? Can anyone see what I''m doing wrong? I suppose
I''ve stared myself blind on this problem by now, so all inputs are
appreaciated :-)

--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}



template hdd::HexGen::RandBits<int>();



This statement obviously lacks a return statement.
When I insert one, I actually gets more error messages:

c:\programfiler\microsoft visual
studio\myprojects\tutorialtests\myxor\hexgen.cpp(3 0) : fatal error C1001:
INTERNAL COMPILER ERROR
(compiler file ''msc1.cpp'', line 1794)

c:\programfiler\microsoft visual
studio\myprojects\tutorialtests\myxor\main.cpp(12) : error C2062: type
''long'' unexpected
Error executing cl.exe.


这篇关于使用功能模板的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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