Inkyu Lee Inkyu Lee - 1 month ago 8
C++ Question

c++ strcpy SIGINT error

I'm a student and I'm learning c++ language.

Here is my class;

class Book
{
private:
int location;
char bookNum;
char bookName[100];
}


I'm going to set string bookName "DEFAULT" by constructor

#include "Bookclass.h"

using namespace std;

Book::Book()
:location(0), bookNum('0')
{
strcpy(bookName, "DEFAULT");
cout << "default constructor" << endl;
}


and an error happened on strcpy in the constructor. When I used debugger(Dev c++), it said me that a SIGINT error happened. When I run it, it stop running.

Tas Tas
Answer

One of the most amazing things about C++ is the standard library, which has solved all these cumbersome code issues. Consider replacing bookName with a std::string:

#include <string>

class Book
{
    Book();
private:
    int location;
    char bookNum;
    std::string bookName;
}

Book::Book()
    :location(0), bookNum('0'), bookName("DEFAULT")
{
        cout << "default constructor" << endl;
}

bookName will be set to "DEFAULT" in the constructor, and reassigning it is as simple as bookName = "new book name";

Comments