some help here.

This is a discussion on some help here. within the C++ Programming forums, part of the General Programming Boards category; Code: int main () { int Choise; cout <<"Welcome! "<< endl; string loser =""; employee *Eptr = new workman (loser,loser,0,0); ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    some help here.

    Code:
    int main ()
    {
      int Choise;
      cout <<"Welcome! "<< endl;
      string loser ="";
      employee *Eptr = new workman (loser,loser,0,0);
    
      return 0;
    }
    compiles and runs fine.

    Code:
    int main ()
    {  int Choise;
      cout <<"Welcome! "<< endl;
    
      employee *Eptr = new workman ("","",0,0);
    
      return 0;
    }
    does not compile and gives error:

    "main.cpp": E2285 Could not find a match for 'workman::workman(char *,char *,int,int)' in function main() at line 31


    why the ........ does this not compile!? it is like? the same thing annyway!

    workman (string &fname,string &lname,int hours, double salary); => workman constructor
    employee ( string &, string &, int = 0 , double = 0 ); => employee constructor

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    "" is a char*; string("") is a string. Use this
    Code:
    employee *Eptr = new workman (string(""),string(""),0,0);
    or better yet overload employee() to support this
    Code:
    workman (char *fname,char *lname,int hours, double salary);
    or maybe const char *s.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Your constructor takes a string reference that is not to const and a string literal cannot be converted to a plain reference. Make it const string& in your constructors.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    It's not the same thing. "string" is the name of a C++ class, while "" defines a constant character array.
    Code:
    int main ()
    {  int Choise;
      cout <<"Welcome! "<< endl;
    
      employee *Eptr = new workman ("","",0,0);
    
      return 0;
    }
    is the same as
    Code:
    int main ()
    {  int Choise;
      cout <<"Welcome! "<< endl;
      const char *loser ="";
      employee *Eptr = new workman (loser,loser,0,0);
    
      return 0;
    }
    Edit: whoah, that was quick.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    >> char *loser ="";
    This should be const char* loser = "".

    But simply adding the const to the reference for the constructor parameters is the only change that needs to be made.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    ah yes, i was relooking in my book and it also shows a const string... ty guys :P that was quite quicky :P

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21