简体   繁体   中英

Ambiguous constructor

Well, I'm newbie to C++ and I was practicing with constructors. I'm creating a bad version of String class and I have been asked to do the next task:

a) Create a constructor where you can make a conversion from const char* to String .

b) Create a constructor with the 'n' first letters from a const char* . If 'n' is longer than this const char* , a copy of this const char* will be written as String . If 'n == 0', the program will write an empty String .

I think I had no problems implementing them; this is what I 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 i = 0; i < tam_; i++){

        s_[i] = cad[i];
    }

    s_[tam_] = '\0';
}

My problem comes when I try to test them in my main method, where I get this error: "c1 is ambiguous".

I tried making this with a dummy parameter (declaring my unsigned parameter as an int with no name in my header) but I would like to initizialite this second parameter to 0 and I can't make this using a dummy parameter.

I know that the compiler doesn't know which constructor must use, but I would like to do this somehow. Can somebody help me? Sorry for my bad English.

I'm creating a bad version of String class...

NO NO NO . This is never something you should do. You say you're a beginner to c++, that doesn't mean you will be writing "bad" code, maybe it's naive, long, has bad syntax, and is not the best solution but that doesn't make it "bad". Also, if you really think that your string class is "bad" than i suggest you redo it. You're in the process of learning, don't intentionally write code that you know is below your standard. How you code now will influence how you write "better" code in the future even if you think it won't.

So please, for your benefit and for future people who will be working with your code, don't ever try and get away with "bad" code. Cheers!

Actually, you only need one constructor. Also, what you are doing in both function is basically the same, so you can call one from the other if you really want two function. Instead of having two separate Cadena::Cadena(const char* cad) and Cadena::Cadena(const char* cad, unsigned lon), you can just use Cadena::Cadena(const char* cad, unsigned lon = 0), and you can just call Cadena tmp(c), and it will work.

So constructor is going to be

Cadena::Cadena(const char* cad, unsigned int lon = 0) {
    tam_ = (lon > 0 ? lon: strlen(cad));
    s_ = new char[tam_ + 1];

    strcpy(s_, cad);
    s_[tam_] = '\0';
}

and your main/tester can be called like this.

int main()
{
    char s[] = "Hello";
    Cadena c(s);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM