473,320 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Warning with template functions

105 100+
Hi all,

I have a template function which is member of this class and I am calling this function in of my other member functions of the same class. But I am getting the warning as

Expand|Select|Wrap|Line Numbers
  1. see reference to function template instantiation 'T script_out::SUMMING<float>(T,std::string,T)' being compiled
  2. 1>        with
  3. 1>        [
  4. 1>            T=float
  5. 1>    ] 
  6.  
Summing is my template function and I am calling it in another member function of my class as

Expand|Select|Wrap|Line Numbers
  1. Fout<<SUMMING<float>(10,file1,37);
  2.  
I am defining this template function as usual in cpp file (not in the header file)

In the header file I declared as

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2.     T SUMMING (T rad,std::string filename,T level);
  3.  
In cpp file

Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. T script:: SUMMING (T rad,std::string filename,T level)
  3. {
  4.  
  5. Rest of the code
  6.  
  7. }
  8.  
Script is my class.

Thanks.
Apr 25 '07 #1
10 6028
mickey22
105 100+
Could anyone help me with this warning.I am stuck at thi point.

Thanks
Apr 26 '07 #2
AdrianH
1,251 Expert 1GB
Could anyone help me with this warning.I am stuck at thi point.

Thanks
The problem is due to how current C++ compilers work with templates. All templates and their bodies must be accessible to the code that is using it. That means that you cannot have the declaration and the definition separated in to header and source files (though you can have them separated) like you would have for non-template classes and functions.

Either throw them in to the same file or put the template functions in another file and have the header file include that file.

Let me know if you are still having trouble.


Adrian
Apr 26 '07 #3
mickey22
105 100+
Adrian,

I tried to include the declaration of the function template in my header file itself.But still I am getting the error.

Actually in my program I am also using one more class template in my function template.This class template is written by somebody.So my guess is this warning starts from this class template.To use this class template I included its header file in my program.

I am using INTEL C++ compiler (on windows).

Please let me know if you have any idea of how to remove this or let me know if I am not clear with my explanation.

Thanks
Apr 26 '07 #4
AdrianH
1,251 Expert 1GB
I am assuming that the template class you are refering to is written with the same constraints as I specified (declaration in same file as definition)? If it is, then please copy and paste up to the first 5 errors/warnings emitted by the compiler in to your next post.

Is this new code or old and tested so it should work? If it is new, post the code at the offending line, plus the function around it. Please mark the line number at the offending line with a comment.

With luck I'll be able to tell you what went wrong.


Adrian
Apr 27 '07 #5
mickey22
105 100+
Adrian,

Expand|Select|Wrap|Line Numbers
  1. d:\ test.cpp(450) : warning C4244: 'argument' : conversion from 'uint64' to 'size_t', possible loss of data
  2. 1>        d:\test.cpp(424) : while compiling class template member function 'void test<T>::read(const uint64,T *const ,const uint64)'
  3. 1>        with
  4. 1>        [
  5. 1>            T=float
  6. 1>        ]
  7. 1>        .\script.cpp(617) : see reference to class template instantiation 'test<T>' being compiled
  8. 1>        with
  9. 1>        [
  10. 1>            T=float
  11. 1>        ]
  12. 1>        .\script.cpp(735) : see reference to function template instantiation 'T script::SUMMING<float>(T,std::string,T)' being compiled
  13. 1>        with
  14. 1>        [
  15. 1>            T=float
  16. 1>    ]
  17.  
------------------------------------------------------------------------

SUMMING is the member function of my class script and test is the class created by someone and I am trying to utilise this class.

I just tried to call this function in one of my member functions of my classThis is where it is showing the warning1 at this line:

Expand|Select|Wrap|Line Numbers
  1. fout<<"sum of the first case is: "<<SUMMING<float>(10,filename1,32)<<endl;

-----------------------------------------------------------------------------
An the other warning is being shown in my function SUMMING at this line:

Expand|Select|Wrap|Line Numbers
  1. Test<float> *ptr=NULL;
  2. ptr=new test <float>(filename,false,false);
Here’s the constructor of the class test (part of the code)

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. test <T>::test(const std::string _filename, const bool _in,
  3.                  const bool endian)
  4.  {     
  5.    filename=_filename;
  6.    block_finished_sem=NULL;
  7.    write_obj=in;
  8. }

--------------------------------------------------------------------------

2nd warning IS SHOWING IN THE TEST CLASS

Read is one of the member function of test class I am utilising in my program

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. void test <T>::read(const uint64 var1, T * const dat, const uint64 size)
  3. ----- here the warning shows at thisline.It points here. { try
  4.    { void *thread;
  5.  
  6.      Try
  7.  
  8. -------rest of the code
  9.  
  10.  
  11. }
Thank you so much for your help .Please let me know if u need any more information form me.

p.s: Actually test class is fully tested and old one and it is advised not to be changed.
Apr 27 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
This warning:
Expand|Select|Wrap|Line Numbers
  1. d:\ test.cpp(450) : warning C4244: 'argument' : conversion from 'uint64' to 'size_t', possible loss of data
  2.  
Tells me you are using Visual Studio.NET.

The issueis that in 64-bit Unix size_t is 64 bits whereas in 64-bit Windows size_t is 32 bits.

Visual Studio is coincerned you may be porting some code from Unix that relies on a 64-bit size-t and the Windows size_t is only 32 bits. Hence the warning.

You can disable the warning by going to your project properties, select The C/C++ in the left pane, then select General in the left pane and then set the property Detect 64-bit Portability issues to NO.
Apr 27 '07 #7
AdrianH
1,251 Expert 1GB
Ok Mickey, I’m having difficulty reading what you posted as you have not given me everything that I asked for, but this is what I understand so far:

You have two classes:
  • script which is a non-template class that you built
  • test is a template class that you are using and shouldn’t have to modify as it has been around a while and has been tested (though the fact it is emitting a warning is a flag to me that it may have need to be revisited).
Inside script is a template function called SUMMING (you really shouldn’t keep this naming convention, it goes against the common naming conventions in C/C++ where all caps indicate a macro, please don’t change it during this discussion, but you may want to consider changing it later).

SUMMING is being called from within script and is where one of the errors is being shown.

Within SUMMING is the creation of a test<float> instance. (I’m not sure about why you are using a float, its precision is limited and can be slower when doing floating point operations on them when compared to doing the same operations on a double. If it is due to memory concerns, then that is your decision. I don’t know your system but premature optimisation based on speed or memory consumption is a pitfall to avoid).

The following is on line 735 in the file script.cpp:
Expand|Select|Wrap|Line Numbers
  1. fout<<"sum of the first case is: "<<SUMMING<float>(10,filename1,32)<<endl;
The following are on lines 616-617 in file script.cpp:
Expand|Select|Wrap|Line Numbers
  1. Test<float> *ptr=NULL;
  2. ptr=new test <float>(filename,false,false);
BTW, if those lines are consecutive, you don’t have to declare the pointer and assign it to NULL then assign it to a new instance. You can assign a new instance directly to the pointer. Doing what you have done is really only necessary if the declaration and the assignment to the new object are separated by a separate scope (like in the body of an if/else/switch/while/for/do and any other construct that I may have forgotten) and you require access to that variable in the outer scope where you have declared it.

Have I missed anything? I must have as I am still not sure what is causing the error.

Is the following exactly how the compiler is emitting the warnings/errors?
Expand|Select|Wrap|Line Numbers
  1.  d:\ test.cpp(450) : warning C4244: 'argument' : conversion from 'uint64' to 'size_t', possible loss of data
  2. 1>        d:\test.cpp(424) : while compiling class template member function 'void test<T>::read(const uint64,T *const ,const uint64)'
  3. 1>        with
  4. 1>        [
  5. 1>            T=float
  6. 1>        ]
  7. 1>        .\script.cpp(617) : see reference to class template instantiation 'test<T>' being compiled
  8. 1>        with
  9. 1>        [
  10. 1>            T=float
  11. 1>        ]
  12. 1>        .\script.cpp(735) : see reference to function template instantiation 'T script::SUMMING<float>(T,std::string,T)' being compiled
  13. 1>        with
  14. 1>        [
  15. 1>            T=float
  16. 1>    ]
  17.  
It feels like this may be only part of it and/or is being processed by the environment. It could be that this is one warning and the rest are for tracing when the instance of the template is being emitted. But since you didn’t give me a contextual annotated location from where the errors occurred, it is kinda difficult to determine.

Could you please ensure that you are copying from the console output of the environment that you are using? Also, copy the compiler output from the top of the window, I’d like to see right from the beginning (usually there is some sort of copyright message on the first few lines, I want to see those as well as this will tell me that I’ve gotten everything).

Also, if you could post the code snippets again with more context and with the filename before the snippet and the line numbers as a comment to either the left or right of the line, it would be helpful. If you haven’t erased lines in between, you needn’t add line numbers to every line, but if you do skip lines, put the line number where you restarted. Since the test class is old and has been tested previously, you need not repost that one. The signature (prototype) that you provided is sufficant.

Sorry for asking you for all of this, but this problem is not a simple one and without context and other specific information, it makes it very difficult to diagnose it.

Further, please use [code][/code] tags, I don’t like having to add them to someone else’s work, as it make more work for me and give me less time to work on your problem.

TTYS,


Adrian
Apr 27 '07 #8
AdrianH
1,251 Expert 1GB
Oh, one other thing. Sometimes it can help when debugging templates to create a non-template concrete method instead. Template debugging can get a bit hairy. :) Try that and see if it gets you anywhere.


Adrian
Apr 28 '07 #9
mickey22
105 100+
Adrian,

My expectations is that there might be an arroe in the laready tested code.

I n the tested code ther is aline that uses a fread function sand there is not type conversuion from unit 64 to size_t as fread returns size_type.So I just tried typecasting it and it removed that warning.But I am not supposed to change the code.

I have to make sure if thats the bug or not.Probably I will find out and if I try to find any solution I will post it back.

Thank you so much for you help.I really appreciate it.
May 1 '07 #10
AdrianH
1,251 Expert 1GB
Adrian,

My expectations is that there might be an arroe in the laready tested code.

I n the tested code ther is aline that uses a fread function sand there is not type conversuion from unit 64 to size_t as fread returns size_type.So I just tried typecasting it and it removed that warning.But I am not supposed to change the code.

I have to make sure if thats the bug or not.Probably I will find out and if I try to find any solution I will post it back.

Thank you so much for you help.I really appreciate it.
I'm so sorry, I misread your initial post. I was thinking that this was an error, and was why I was asking for more info.

Expand|Select|Wrap|Line Numbers
  1. [d:\ test.cpp(450) : warning C4244: 'argument' : conversion from 'uint64' to 'size_t', possible loss of data
  2. 1>        d:\test.cpp(424) : while compiling class template member function 'void test<T>::read(const uint64,T *const ,const uint64)'
  3. 1>        with
  4. 1>        [
  5. 1>            T=float
  6. 1>        ]
  7. 1>        .\script.cpp(617) : see reference to class template instantiation 'test<T>' being compiled
  8. 1>        with
  9. 1>        [
  10. 1>            T=float
  11. 1>        ]
  12. 1>        .\script.cpp(735) : see reference to function template instantiation 'T script::SUMMING<float>(T,std::string,T)' being compiled
  13. 1>        with
  14. 1>        [
  15. 1>            T=float
  16. 1>    ]
Given what you said and the warning, it would appear that you are correct in your assesment. I could be wrong but I think you meant size_t not size_type in your last post.

size_t is a type ment to be redefinable as operating systems have a tendency of increasing their capacity. Most compilers and their libraries are currently set to an unsigned 32 bit value.

Because the test class is using an unsigned 64 bit value, it is loosing some of its percision when being converted to a size_t type. Unless the unsigned 64 bit value passed is greater than what a unsigned 32 bit value can hold (~4.3GiB), this will not cause a problem. I expect that the original developer didn't/doesn't think that this would ever occur.

For maximum safety, you should in addition to casting to the parameter to size_t (indicating to the compiler that you are aware of the problem); you should also code an assert prior to calling fread() to ensure that the case I described does not occur, however unlikely. This is because system's life spans sometimes exceed what the original developer anticipated (think Y2K bug).

The maximum value for size_t is numeric_limits<size_t>::max() (see limits library)

Good luck,


Adrian
May 1 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
1
by: Alf P. Steinbach | last post by:
Microsoft Visual C++ warning C4267 (the number is not a typo) can occur e.g. when compiling Boost Spirit, or when doing std::size_t x = 0; std::cout << x; when compiled with 64-bit...
1
by: Ian | last post by:
I've just discovered the msclr::lock class in the C++ Support Library online documentation. This seems like a much cleaner way to implement thread protection than using...
0
by: Jim Avera | last post by:
On AIX, a template class containing a nested class which needs to be a friend provokes a warning: (W) A template dependent name that is a type must be qualified with "typename". and method...
5
by: holmescn | last post by:
what is the meaning of warning attributes ignored on template instantiation. i got it when i compiled stlport 5.1.3. anybody can help me ? thx!
13
by: Anonymous | last post by:
On MS site: http://msdn2.microsoft.com/en-us/library/esew7y1w(VS.80).aspx is the following garbled rambling: "You can avoid exporting classes by defining a DLL that defines a class with...
6
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hello, I have a class hierarchy distributed over 3 native C++ dlls. The base class has a .NET Windows.Form for status output via a gcroot<>. The gcroot is declared private - the sub classes only...
1
by: krey | last post by:
Good Day, after reading nice book "Genering Programming" by Czarnecki & Eisenecker i tried to extend an example with static lists. I tried to add a metafunction for calculating max element from...
11
by: Jeff | last post by:
I turned on errors in php: ini_set('display_errors','1'); And I got a slew of notices and a couple of warnings. The notices are mostly missing indexes from doing things like this: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.