简体   繁体   中英

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.

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";

Your code works as intended on my PC, as such, the code is not the problem, your compiler and/or IDE is at fault here.

Look here , SIGINT means an interruption from keyboard.

I recommend changing your IDE to Visual Studio or NetBeans with C++.

Also, in all honesty this is C with classes, not C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM