error:no appropriate constructor found, even though i have made one,c++











up vote
2
down vote

favorite
1












class String
{
private:
static const int SZ = 80;
char str[SZ];
public:
String()
{
strcpy_s(str, " ");
}
String(char s)
{
strcpy_s(str, s);
}
}


This is the constructor i have written, note the second one



and here I am using it:



String s1 = "yes";
String s2 = "no";
String s3;


This is the error message :




Severity Code Description Project File Line Suppression State Error
(active) E0415 no suitable constructor exists to convert from "const
char [4]" to "String" rishabh c++ projects F:rishabh c++
projectsrishabh c++ projectsmain.cpp 35











share|improve this question




















  • 5




    Note the error message said const char[4].
    – songyuanyao
    Nov 7 at 9:39















up vote
2
down vote

favorite
1












class String
{
private:
static const int SZ = 80;
char str[SZ];
public:
String()
{
strcpy_s(str, " ");
}
String(char s)
{
strcpy_s(str, s);
}
}


This is the constructor i have written, note the second one



and here I am using it:



String s1 = "yes";
String s2 = "no";
String s3;


This is the error message :




Severity Code Description Project File Line Suppression State Error
(active) E0415 no suitable constructor exists to convert from "const
char [4]" to "String" rishabh c++ projects F:rishabh c++
projectsrishabh c++ projectsmain.cpp 35











share|improve this question




















  • 5




    Note the error message said const char[4].
    – songyuanyao
    Nov 7 at 9:39













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





class String
{
private:
static const int SZ = 80;
char str[SZ];
public:
String()
{
strcpy_s(str, " ");
}
String(char s)
{
strcpy_s(str, s);
}
}


This is the constructor i have written, note the second one



and here I am using it:



String s1 = "yes";
String s2 = "no";
String s3;


This is the error message :




Severity Code Description Project File Line Suppression State Error
(active) E0415 no suitable constructor exists to convert from "const
char [4]" to "String" rishabh c++ projects F:rishabh c++
projectsrishabh c++ projectsmain.cpp 35











share|improve this question















class String
{
private:
static const int SZ = 80;
char str[SZ];
public:
String()
{
strcpy_s(str, " ");
}
String(char s)
{
strcpy_s(str, s);
}
}


This is the constructor i have written, note the second one



and here I am using it:



String s1 = "yes";
String s2 = "no";
String s3;


This is the error message :




Severity Code Description Project File Line Suppression State Error
(active) E0415 no suitable constructor exists to convert from "const
char [4]" to "String" rishabh c++ projects F:rishabh c++
projectsrishabh c++ projectsmain.cpp 35








c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 10:23









Luboš Turek

2,09442338




2,09442338










asked Nov 7 at 9:36









rishabh jain

133




133








  • 5




    Note the error message said const char[4].
    – songyuanyao
    Nov 7 at 9:39














  • 5




    Note the error message said const char[4].
    – songyuanyao
    Nov 7 at 9:39








5




5




Note the error message said const char[4].
– songyuanyao
Nov 7 at 9:39




Note the error message said const char[4].
– songyuanyao
Nov 7 at 9:39












1 Answer
1






active

oldest

votes

















up vote
6
down vote



accepted










The compiler is telling you there is no implicit cast from your string literal to any of the available constructors. The reason behind this is the types that your class can be constructed with:



String(char s);


Note that string literals are constant, as per the C++ standard. That will not be implicitly converted to the non-constant type that your constructor requires. To do that would require const_cast, but please don't do that!!!



You would also get an error (or at least a warning) if you attempted explicit construction:



String s1("yes");  // error


The simple fix is the change the parameter to const char (or indeed const char* is more commonly used).



String(const char *s)
{
strcpy_s(str, s);
}


Whenever passing pointer or reference types as parameters, always ask yourself: "does my function need to modify the value of the parameter?" -- whenever the answer is "no" make it const.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186774%2ferrorno-appropriate-constructor-found-even-though-i-have-made-one-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    6
    down vote



    accepted










    The compiler is telling you there is no implicit cast from your string literal to any of the available constructors. The reason behind this is the types that your class can be constructed with:



    String(char s);


    Note that string literals are constant, as per the C++ standard. That will not be implicitly converted to the non-constant type that your constructor requires. To do that would require const_cast, but please don't do that!!!



    You would also get an error (or at least a warning) if you attempted explicit construction:



    String s1("yes");  // error


    The simple fix is the change the parameter to const char (or indeed const char* is more commonly used).



    String(const char *s)
    {
    strcpy_s(str, s);
    }


    Whenever passing pointer or reference types as parameters, always ask yourself: "does my function need to modify the value of the parameter?" -- whenever the answer is "no" make it const.






    share|improve this answer

























      up vote
      6
      down vote



      accepted










      The compiler is telling you there is no implicit cast from your string literal to any of the available constructors. The reason behind this is the types that your class can be constructed with:



      String(char s);


      Note that string literals are constant, as per the C++ standard. That will not be implicitly converted to the non-constant type that your constructor requires. To do that would require const_cast, but please don't do that!!!



      You would also get an error (or at least a warning) if you attempted explicit construction:



      String s1("yes");  // error


      The simple fix is the change the parameter to const char (or indeed const char* is more commonly used).



      String(const char *s)
      {
      strcpy_s(str, s);
      }


      Whenever passing pointer or reference types as parameters, always ask yourself: "does my function need to modify the value of the parameter?" -- whenever the answer is "no" make it const.






      share|improve this answer























        up vote
        6
        down vote



        accepted







        up vote
        6
        down vote



        accepted






        The compiler is telling you there is no implicit cast from your string literal to any of the available constructors. The reason behind this is the types that your class can be constructed with:



        String(char s);


        Note that string literals are constant, as per the C++ standard. That will not be implicitly converted to the non-constant type that your constructor requires. To do that would require const_cast, but please don't do that!!!



        You would also get an error (or at least a warning) if you attempted explicit construction:



        String s1("yes");  // error


        The simple fix is the change the parameter to const char (or indeed const char* is more commonly used).



        String(const char *s)
        {
        strcpy_s(str, s);
        }


        Whenever passing pointer or reference types as parameters, always ask yourself: "does my function need to modify the value of the parameter?" -- whenever the answer is "no" make it const.






        share|improve this answer












        The compiler is telling you there is no implicit cast from your string literal to any of the available constructors. The reason behind this is the types that your class can be constructed with:



        String(char s);


        Note that string literals are constant, as per the C++ standard. That will not be implicitly converted to the non-constant type that your constructor requires. To do that would require const_cast, but please don't do that!!!



        You would also get an error (or at least a warning) if you attempted explicit construction:



        String s1("yes");  // error


        The simple fix is the change the parameter to const char (or indeed const char* is more commonly used).



        String(const char *s)
        {
        strcpy_s(str, s);
        }


        Whenever passing pointer or reference types as parameters, always ask yourself: "does my function need to modify the value of the parameter?" -- whenever the answer is "no" make it const.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 9:50









        paddy

        41.8k52976




        41.8k52976






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186774%2ferrorno-appropriate-constructor-found-even-though-i-have-made-one-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini