error: none of the 2 overloads can convert parameter 1 from type 'char [7]'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 0609mao
    New Member
    • Sep 2011
    • 1

    error: none of the 2 overloads can convert parameter 1 from type 'char [7]'

    #include<iostre am>
    #include<string >
    using namespace std;
    class Class1
    {
    public :
    Class1(char* p){};

    };

    class Class2
    {
    public:
    Class2(char* p){ };
    };

    void f(Class1&x){};
    void f(Class2&x){};

    void main()
    {
    f("string");
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Both overloads of function f take a class object by reference. That means the overloads will work only if you have already created a
    Class1 or Class2 object and use that object on the call to f.

    By using a string on the call to f you need an overload that is not a reference. That will cause the object to be created by the compiler and then passed to f. Be aware, though, that if you do this the compiler will have a choice to create a Class1 or a Class2 object. The compiler will error at this point with an ambiguous overload.

    Comment

    Working...