1

I have tried this:

int n;
cin >> n;
int * pn = new int[n];

Then I can use the array lik this: pn[i] = ...

But I would like to organize something of the kind with my own class:

class MyString
{
    char* m_pStr;
    public:
    MyString(const char* pStr){ 
        this->CreateArray(pStr);
        strcpy(m_pStr, pStr);
    }
}

void main(){
    int N;
    cout << "Iinput N: ";
    cin >> N;
    MyString * ar = new MyString[N];

    char tmp[100];
    for (int i = 0; i < 4; i++){
        cout <<"Input the string for the Mystring " << i + 1 << " ";
        cin >> tmp;     
        ar[i] = MyString(tmp);
    }
}

I get: error C2512: 'MyString' : no appropriate default constructor available.

dckuehn
  • 2,427
  • 3
  • 27
  • 37
Pepperwork
  • 105
  • 2
  • 4
  • 12
  • Please read a Good Introductory book on C++. Here is few recommendations : [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Nawaz Mar 10 '13 at 07:10
  • 2
    When you are using C++, NEVER use C-strings. Always use `string`. ALWAYS. – naxchange Mar 10 '13 at 07:14
  • The very words in the error message couldn't possibly make it clearer what the problem is. `MyString` has no default constructor, and the code you wrote requires one. (and in truth, the code you wrote needs std::string and belay all the rest). – WhozCraig Mar 10 '13 at 07:15
  • possible duplicate of [When do we need to have a default constructor?](http://stackoverflow.com/questions/5498937/when-do-we-need-to-have-a-default-constructor) – WhozCraig Mar 10 '13 at 07:18
  • What is `CreateArray(...)`? – naxchange Mar 10 '13 at 07:24
  • If you are going to ask for help please take time to format the code nicely. You are nor using C++ well here. You should prefer automatic objects to dynamic ones. If you don't know the difference a good book should be your next stop. – Martin York Mar 10 '13 at 19:27
  • What you need to do is to read some basic books. – Yves Nov 17 '16 at 00:49

3 Answers3

0

You are trying to instantiate the MyString class with the default constructor without having defined one in this line:

MyString * ar = new MyString[N];

It has to initialize the objects and will therefore try to use the default constructor that is not set.

I would personally change your constructor from:

MyString(const char* pStr);

to:

MyString(const char* pStr = "");

in order to act like the default one.

Shoe
  • 74,840
  • 36
  • 166
  • 272
0

You forgot to include the default constructor!

MyString() : m_pStr(0) { }

That error occurred when you tried to instantiate the array of MyStrings from here:

MyString * ar = new MyString[N];

Which looks for a default constructor. Since you hadn't defined one in your class definition, you got the error that you described.

Hence, to solve your problems, create default constructor for your class, and everything should compile as it should.

See this link for more details (or read up on some C++):

http://en.wikipedia.org/wiki/Default_constructor

jrd1
  • 10,358
  • 4
  • 34
  • 51
0

Ok, there is a lot that I would change in the code above.

First, the answer to your question: the default constructor is not always needed. In fact, if you do not define any constructor method, then the default constructor is invoked automatically. However, since you defined a constructor with arguments, then you need to define its behaviour. If you don't want it to be invoked in your code, define it under private in your class definition. Now, once you fix that, you are going to run into other problems:

  1. this is a pointer to the current object, and you are trying to dereference it and get the member function CreateArray, which doesn't exist.

  2. Never use C-style strings when coding in C++, because you can create string objects, and if you ever need the c-style string, you can retrieve it with a method of the string class called c_str().

  3. Don't use void main. Not all compilers allow it

  4. Free the memory used by the dynamically created array, using delete [] ar

So, the code has a bunch of problems. I think the real issue here, is that you probably know some C, and you are rushing to learn C++, but please keep in mind that learning a language properly takes patience, and just because your code might work, doesn't mean it's good code, and you should put in some more effort if you want to properly learn a language. Trust me, I've learned the hard way!

naxchange
  • 893
  • 1
  • 11
  • 24