c++ - Ambiguous constructor -


well, i'm newbie c++ , practicing constructors. i'm creating bad version of string class , have been asked next task:

a) create constructor can make conversion const char* string.

b) create constructor 'n' first letters const char*. if 'n' longer const char*, copy of const char* written string. if 'n == 0', program write empty string.

i think had no problems implementing them; have:

cadena::cadena(const char* cad){      tam_ = strlen(cad);     s_ = new char[tam_ + 1];      strcpy(s_,cad); }  cadena::cadena(const char* cad, unsigned lon){      tam_ = lon;     s_ = new char[tam_ + 1];      for(unsigned = 0; < tam_; i++){          s_[i] = cad[i];     }      s_[tam_] = '\0'; } 

my problem comes when try test them in main method, error: "c1 ambiguous".

i tried making dummy parameter (declaring unsigned parameter int no name in header) initizialite second parameter 0 , can't make using dummy parameter.

i know compiler doesn't know constructor must use, somehow. can me? sorry bad english.

i'm creating bad version of string class...

no no no. never should do. you're beginner c++, doesn't mean writing "bad" code, maybe it's naive, long, has bad syntax, , not best solution doesn't make "bad". also, if think string class "bad" suggest redo it. you're in process of learning, don't intentionally write code know below standard. how code influence how write "better" code in future if think won't.

so please, benefit , future people working code, don't ever try , away "bad" code. cheers!


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -