1

First of all, I am not a very experimented C++ programmer, I am just trying to pass my object oriented programming exam. I know that if I have a class constructor with only one parameter, If I do something like this -> myClass object = something is like passing something to the constructor with one parameter, right? Then, why does this work fine:

#include <iostream>
using namespace std;

class A
{
public:
    int m;

    A(int M){
        m = M;
    }

    print(){
        cout << "m: " << m << "\n";
    }
};

int main()
{
    A x = 132;
    x.print();

return 0;
}

and this does not:

#include <iostream>
using namespace std;

class A
{
public:
    string m;

    A(string M){
        m = M;
    }

    print(){
        cout << "m: " << m << "\n";
    }
};

int main()
{
    A x = "RANDOMSTRING";
    x.print();

return 0;
}

The error I get in the second case is: error: conversion from 'const char [13]' to non-scalar type 'A' requested

I guess it's some kind of problem with that string parameter, but why ?

Johnny
  • 447
  • 2
  • 8

0 Answers0