Thread: Template/classes/strings

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    9

    Template/classes/strings

    I have this relatively easy asignment for school (only thing is that "tutorials" for C++ on the internet are TOO DAMN COMPLICATED so I bearly understand a thing :|)

    I get the errors
    Code:
    In member function `PACKAGE DATE_EXP<PACKAGE>::INSERT_DATE_EXP() [with PACKAGE = char]': 
     instantiated from here 
    invalid conversion from `char' to `char*' 
    initializing argument 1 of `char* gets(char*)'
    because of the "gets(NAME_EXP);" thing but I need to use it to put something like "bla bla text with spaces" because string normally doesn't accept spaces


    Code:
    template <class PACKAGE>
    class DATE_EXP{
    PACKAGE NAME_EXP    ,ADRESS_EXP      ;
    public:
    DATE_EXP(PACKAGE first,PACKAGE second){NAME_EXP=first,ADRESS_EXP=second;}
    PACKAGE INSERT_DATE_EXP ();
    }; //class DATE_EXP
    template <class PACKAGE>
    PACKAGE DATE_EXP<PACKAGE>::INSERT_DATE_EXP            (){
    gets(NAME_EXP);
    gets(ADRESS_EXP);
    cout<<"NAME_EXP IS "<<NAME_EXP<<"\n";
    cout<<"ADRESS_EXP IS "<<ADRESS_EXP<<"\n";
    
    main(){
    
    char NAME_EXP_TEMP,ADRESS_EXP_TEMP;
    DATE_EXP<char>EXP_TEMP(NAME_EXP_TEMP,ADRESS_EXP_TEMP);
    cout<<EXP_TEMP.INSERT_DATE_EXP();
    
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No using gets!

    Read up on the wonderful world of iomanip.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    9
    sorry, I don't get it

    you just sent me back to the place with lots of complicated "tutorials" that I don't understand

    could you at least point out which one of those functions/commands/whatever they are called, I need to use? "noskipws"? "ws"?

    why am I not supposed to use "gets()" anyway, what's wrong with it?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    gets() is a worm, pretty literally. It does not respect the boundaries between the variable you intend to put it in, and other variables in memory, or other people's memory, etc.

    Second, you need to learn the difference between a "tutorial" and a "reference". Although you do have a legitimate complaint, because I linked to the wrong page: getline - C++ Reference is where you're supposed to be. (I'd blame the pain meds, but it's only ibuprofen. Sorry.)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A common convention is to reserve fully capitalised names for macro names (though T is also a conventional template parameter name, but here Package would probably be better). It is good that you posted using code bbcode tags, but you should indent your code properly too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that the return type for main is missing. This is illegal C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    9
    ok I got it now thanks, but for

    Code:
    template <class PACKAGE>
    class KG_GC{
    PACKAGE KG,GC;
    public:
    KG_GC(PACKAGE first,PACKAGE second){KG=first,GC=second;}
    PACKAGE INSERT_KG_GC ();}; //class KG_GC
    template <class PACKAGE>
    PACKAGE KG_GC<PACKAGE>::INSERT_KG_GC (){
    cin>>KG;
    cin>>GC;
    cout<<"KG:"<<KG<<"\n";
    cout<<"GC:"<<GC<<"\n";};
    
    
    main(){
    
    double KG_TEMP,GC_TEMP;
    KG_GC<double>KG_GC_TEMP(KG_TEMP,GC_TEMP);
    cout<<KG_GC_TEMP.INSERT_KG_GC ();
    }
    it says "1.#QNAN" at the end, how do I get rid of it?

    P.S. I used all caps because none of it is final, it's just sketchy stuff, and these are just parts of a bigger program which doesn't require return 0 (that's another story)

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ...Did you listen to what was suggested here?
    The code is a mess. Fix it!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    9
    sorry, how is it a mess besides using for example <class PACKAGE> instead of <class T>?

    how would I know how to fix something I don't know how to fix, for example "1.#QNAN" doesn't tell me anything and I try to search the internet but it doesn't tell me much

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You fix it by listening to comments made by members. Here are two helpful ones:
    Quote Originally Posted by laserlight View Post
    A common convention is to reserve fully capitalised names for macro names (though T is also a conventional template parameter name, but here Package would probably be better). It is good that you posted using code bbcode tags, but you should indent your code properly too.
    Quote Originally Posted by Elysia View Post
    Note that the return type for main is missing. This is illegal C++.
    Note the parts in bold.
    Also, stop putting the ending braces on the same line. That is just obfuscating the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you look up your "number" you should find things about NaN = Not a Number. Somewhere along the way you have done something awful in your calculations (or perhaps you are just interpreting some random check of memory as a double when it isn't; hard to say).

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    9
    thanks but no thanks, I understand that it would be easier for everyone else to understand if I try to put every little thing in the same standard as everyone else, but sorry again, I prefer laying all those little things differently... the program does the exact same thing anyway

    as for return, this is how I do it:

    Code:
    ...
    int main(){
    ...
    do{
    ...
    int B0;cin>>B0;switch(B0){
    ...
    case  0:{return 0;}
    ...
     break;}//switch B0=18
     }//switch B0
     }while(1);//doodoo
    
     }//main

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by llVIU
    thanks but no thanks, I understand that it would be easier for everyone else to understand if I try to put every little thing in the same standard as everyone else, but sorry again, I prefer laying all those little things differently... the program does the exact same thing anyway
    Unless you are working for a company or on a project that mandates a specific style, there is no standard style. However, there are commonly used styles and conventions. Pick one such style, and follow conventions when you do not have a good and compelling reason to do otherwise. If not, your code will be more difficult to read and understand, and possibly also more difficult to edit. This may not matter to you as a student, but it will matter should you do group projects and later work in a team after you graduate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    thanks but no thanks, I understand that it would be easier for everyone else to understand if I try to put every little thing in the same standard as everyone else, but sorry again, I prefer laying all those little things differently... the program does the exact same thing anyway
    O_o

    Came by to answer your "how do I get rid of it?" question, but then I read that post.

    If you can't be bother to help yourself why should we be bothered to help you?

    Soma

  15. #15
    Registered User
    Join Date
    May 2011
    Posts
    9
    look, please stop with the irrelevant talk, does anyone know how I can fix the "1.#QNAN" error or not? I'd very much appreciate if I could be helped only with the "1.#QNAN" error and nothing else, *NOTHING* else please, thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance and Template Classes
    By brunion1 in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2011, 02:23 AM
  2. Specifying Allowed Template Classes
    By nine-hundred in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2007, 12:22 AM
  3. templated members in non-template classes
    By drrngrvy in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2007, 01:38 PM
  4. Passing Template values on to other classes
    By vinsta18 in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2004, 05:26 PM
  5. Template Classes
    By AdioKIP in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2002, 06:13 PM