Warning with template functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey22
    New Member
    • Feb 2007
    • 105

    Warning with template functions

    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

    Code:
    see reference to function template instantiation 'T script_out::SUMMING<float>(T,std::string,T)' being compiled
    1>        with
    1>        [
    1>            T=float
    1>	]
    Summing is my template function and I am calling it in another member function of my class as

    Code:
    Fout<<SUMMING<float>(10,file1,37);
    I am defining this template function as usual in cpp file (not in the header file)

    In the header file I declared as

    Code:
    template <class T>
        T SUMMING (T rad,std::string filename,T level);
    In cpp file

    Code:
    template <class T>
    T script:: SUMMING (T rad,std::string filename,T level)
    {
    
    Rest of the code
    
    }
    Script is my class.

    Thanks.
    Last edited by AdrianH; Apr 26 '07, 02:31 PM. Reason: Added [code][/code] tags
  • mickey22
    New Member
    • Feb 2007
    • 105

    #2
    Could anyone help me with this warning.I am stuck at thi point.

    Thanks

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by mickey22
      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

      Comment

      • mickey22
        New Member
        • Feb 2007
        • 105

        #4
        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

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          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

          Comment

          • mickey22
            New Member
            • Feb 2007
            • 105

            #6
            Adrian,

            Code:
            d:\ test.cpp(450) : warning C4244: 'argument' : conversion from 'uint64' to 'size_t', possible loss of data
            1>        d:\test.cpp(424) : while compiling class template member function 'void test<T>::read(const uint64,T *const ,const uint64)'
            1>        with
            1>        [
            1>            T=float
            1>        ]
            1>        .\script.cpp(617) : see reference to class template instantiation 'test<T>' being compiled
            1>        with
            1>        [
            1>            T=float
            1>        ]
            1>        .\script.cpp(735) : see reference to function template instantiation 'T script::SUMMING<float>(T,std::string,T)' being compiled
            1>        with
            1>        [
            1>            T=float
            1>	]
            ------------------------------------------------------------------------

            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:

            Code:
            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:

            Code:
            Test<float> *ptr=NULL;
            ptr=new test <float>(filename,false,false);
            Here’s the constructor of the class test (part of the code)

            Code:
            template <typename T>
            test <T>::test(const std::string _filename, const bool _in,
                             const bool endian)
             {     
               filename=_filename;
               block_finished_sem=NULL;
               write_obj=in;
            }

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

            2nd warning IS SHOWING IN THE TEST CLASS

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

            Code:
            template <typename T>
            void test <T>::read(const uint64 var1, T * const dat, const uint64 size)
            ----- here the warning shows at thisline.It points here. { try
               { void *thread;
            
                 Try
            
            -------rest of the code
            
            
            }
            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.
            Last edited by AdrianH; Apr 27 '07, 07:17 PM. Reason: PLEASE use [code][/code] tags. It increases readablility.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              This warning:
              Code:
              d:\ test.cpp(450) : warning C4244: 'argument' : conversion from 'uint64' to 'size_t', possible loss of data
              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.

              Comment

              • AdrianH
                Recognized Expert Top Contributor
                • Feb 2007
                • 1251

                #8
                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:
                Code:
                fout<<"sum of the first case is: "<<SUMMING<float>(10,filename1,32)<<endl;
                The following are on lines 616-617 in file script.cpp:
                Code:
                Test<float> *ptr=NULL;
                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?
                Code:
                 d:\ test.cpp(450) : warning C4244: 'argument' : conversion from 'uint64' to 'size_t', possible loss of data
                1>        d:\test.cpp(424) : while compiling class template member function 'void test<T>::read(const uint64,T *const ,const uint64)'
                1>        with
                1>        [
                1>            T=float
                1>        ]
                1>        .\script.cpp(617) : see reference to class template instantiation 'test<T>' being compiled
                1>        with
                1>        [
                1>            T=float
                1>        ]
                1>        .\script.cpp(735) : see reference to function template instantiation 'T script::SUMMING<float>(T,std::string,T)' being compiled
                1>        with
                1>        [
                1>            T=float
                1>	]
                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

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  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

                  Comment

                  • mickey22
                    New Member
                    • Feb 2007
                    • 105

                    #10
                    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.

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by mickey22
                      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.

                      Code:
                      [d:\ test.cpp(450) : warning C4244: 'argument' : conversion from 'uint64' to 'size_t', possible loss of data
                      1>        d:\test.cpp(424) : while compiling class template member function 'void test<T>::read(const uint64,T *const ,const uint64)'
                      1>        with
                      1>        [
                      1>            T=float
                      1>        ]
                      1>        .\script.cpp(617) : see reference to class template instantiation 'test<T>' being compiled
                      1>        with
                      1>        [
                      1>            T=float
                      1>        ]
                      1>        .\script.cpp(735) : see reference to function template instantiation 'T script::SUMMING<float>(T,std::string,T)' being compiled
                      1>        with
                      1>        [
                      1>            T=float
                      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

                      Comment

                      Working...