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");
} 9 3166
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. 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
This, of course (shame on MS VC 6.0!!!), fixed the problem. It was
my lame compiler that, as you said, didn't support explicite template
specification.
But I also tried using gcc under Linux, and still I got error messages.
So even though at least on of the reasons I got error messages using VC
was that VC does not support explicit template specification, there
seems to be even more wrong with my code...
--
Fred H
void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
} But I also tried using gcc under Linux, and still I got error messages. So even though at least on of the reasons I got error messages using VC was that VC does not support explicit template specification, there seems to be even more wrong with my code...
Would help if you post then those error messages (with lines in which
they occure)...
"Fred H" <se****@nospam.com> wrote in message
news:op**************@news.mimer.no... 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.
The problems you're facing are due to the shortcoming of the VC++ 6.0
compiler regarding templates. However, it can easily be solved by moving the
template declaration from the function to the class, which IMHO makes more
sense anyway. Furthermore it is good practice to implement templates in the
context of the header file and not the .cpp/.cc/.cxx file. There are some
cases using VC++ where separating declaration & definition of templates will
get you strange errors.
namespace hdd {
template<typename T>
struct HexGen {
T 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 //
NOTE: Did you call srand() once beforehand?
}
return bits;
}//EndOf RandBits()
};
}
Regards
Chris The problems you're facing are due to the shortcoming of the VC++ 6.0 compiler regarding templates.
I'm starting to realise this...
However, it can easily be solved by moving the template declaration from the function to the class ...
Ok? It's just that I'm not using templates for the class. Only
for the function. (But I've started putting the template def
in the header file, and not using explicit specification, and
now it works. But...see next paragraph.)
Furthermore it is good practice to implement templates in the context of the header file and not the .cpp ...
But the FAQ suggests that this can cause "significant code bloat".
How about that?
Thanks for all the input btw, all of you :-)
--
Fred H
void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}
On Wed, 04 Feb 2004 16:12:38 GMT, Fred H <se****@nospam.com> wrote: The problems you're facing are due to the shortcoming of the VC++ 6.0 compiler regarding templates.
I'm starting to realise this...
However, it can easily be solved by moving the template declaration from the function to the class ...
Ok? It's just that I'm not using templates for the class. Only for the function. (But I've started putting the template def in the header file, and not using explicit specification, and now it works. But...see next paragraph.)
Furthermore it is good practice to implement templates in the context of the header file and not the .cpp ...
But the FAQ suggests that this can cause "significant code bloat". How about that?
This bloat occurs on compiler/linker combinations that aren't able to
coalesce instantiations of template specializations from different
translation units into a single definition of the specialization in
the final executable. The only system that I have used that has this
problem is GCC on qnx-nto. I doubt you're using anything so obscure!
MSVC, Borland, GCC on normal platforms, Intel, Metrowerks, etc., etc.
don't have this problem - they combine instantiations.
Tom
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html MSVC, Borland, GCC on normal platforms, Intel, Metrowerks, etc., etc. don't have this problem - they combine instantiations.
I kind of recconed that they did, but it's good to have it confirmed.
Thanks!
--
Fred H
void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: AH |
last post by:
I have two functions in my shared library which are declared as
follows:
void setName(const std::string& str);
std::vector<std::string> getInfo();
Since the code is compiled and in shared library. Every thing is great
if the customer of my shared library is using the same compiler
version (and same STL version). I am wondering if the...
|
by: Alan Krueger |
last post by:
Greetings,
I've been able to cache Transformer objects in a Tomcat-based servlet
application to avoid unnecessary Transformer rebuilding, except for
certain ones on certain machines. I'm running Tomcat 4.1.27 under
Eclipse 2.1.0 using the Sysdeo Tomcat plugin using j2re1.4.1_02 under
Windows 2000 SP4.
I've digested this down to a small...
|
by: bjam |
last post by:
Help! The apply-templates function is not currently allowing me to
select a specific template... eventhough I tried putting a select
statement, it does not seem to work??? Can someone help show how I can
select the first template and then have it select others? Below is a
sample of the xml as well as the .xsl file when
I do something like...
|
by: Christian Christmann |
last post by:
Hi,
I want to implement an graph using templates.
In my header file I define the templates node and edge:
template <class NODE> class GNode
{
NODE *info;
public:
GraphNode();
|
by: WittyGuy |
last post by:
Hi,
What is the major difference between function overloading and function
templates?
Thanks!
http://www.gotw.ca/resources/clcm.htm for info about ]
| |
by: Tayfun Özdemir |
last post by:
Hello there,
I have a school project and I have to finish it soon.
I have to implement B+ Tree for the project.
I have TreeNode template class. That will be used to hold keys in the tree
index.
I have LeafNode template class derivered from TreeNode. That will be used to
hold keys and values in leaf.
|
by: Scamjunk |
last post by:
I have been desperately looking for a treeview-type solution for my
problem
for the past three weeks and have been greatly unsuccessful. I am
totally
new to the world of XSLT and I *don't know* JavaScript. Still, I have
managed to get something together, which I am putting across here. Any
help (even pointing me to the place to look) is...
|
by: Josefo |
last post by:
Hello all. I am a newbie following the C++ tutorial in :
http://www.cplusplus.com/doc/tutorial/templates.html
I am unable to succesfully compile any of the examples with templates
of this tutorial. I use the standard c++ compiler which comes with
ubuntu breezy distro. I guess that somethig is wrong with it or (more
likely..) I should use...
|
by: Tim Frink |
last post by:
Hi,
I want to use a callback function together with
templates. Let's say I've this code:
File a.h:
class A
{
private:
|
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. ...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |