Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;-返回字符串时出现分段错误_C++_String_Segmentation Fault - Fatal编程技术网

C++ C++;-返回字符串时出现分段错误

C++ C++;-返回字符串时出现分段错误,c++,string,segmentation-fault,C++,String,Segmentation Fault,我有这门课 #include "Room.h" #include "Book.h" #include <cstdlib> #include <iostream> static int book_counter = 100; //Constructors Book::Book() { for(int i = 0; i < 13; i++) { this->customer_name += 97 + rand() % 26; }

我有这门课

#include "Room.h"
#include "Book.h"
#include <cstdlib>
#include <iostream>

static int book_counter = 100;

//Constructors
Book::Book() {
    for(int i = 0; i < 13; i++) {
        this->customer_name += 97 + rand() % 26;
    }
    this->arrival = rand() % 30;
    this->duration = 1 + (rand() % 10);
    this->ppl = 1 + rand() % 5;
    this->room = nullptr;
    this->book_code = book_counter++;
}
Book::Book(std::string nm, int arr, int dur, int ppl) {
    this->customer_name = nm;
    this->arrival = arr;
    this->duration = dur;
    this->ppl = ppl;
    this->room = nullptr;
    this->book_code = book_counter++;
}

//Methods
int Book::getArr() {
    return arrival;
}
int Book::getDur() {
    return duration;
}
int Book::getPpl() {
    return ppl;
}
void Book::anathesi(Room* x) {
    this->room = x;
}
int Book::getBookCode() {
    return book_code;
}
Room* Book::getRoom() {
    return room;
}
std::string Book::getCustomerName() {
    return customer_name;
}
<>我是C++新手,请详细说明我的错误。

类书的头文件:

#ifndef PROJECT_BOOK_H
#define PROJECT_BOOK_H
#include <string>

class Room;

class Book {
    protected:
        std::string customer_name;
        int book_code;
        int arrival;
        int duration;
        int ppl;
        Room* room;
    public:
        Book();
        Book(std::string nm, int arr, int dur, int ppl);
        void anathesi(Room* x);
        int getArr();
        int getDur();
        int getPpl();
        int getBookCode();
        std::string getCustomerName();
        Room* getRoom();
};

#endif //PROJECT_BOOK_H

发现问题并解决它。 我正在使用这段代码创建实例:

cout << hotel.getBooks(i)->getBookCode() << " " << hotel.getBooks(i)->getCustomerName()
                             << " " << hotel.getBooks(i)->getRoom()->getRoomCode() << "\n";
Book obj(onoma, afiksh - 1, meres, arithmos);
这导致了我所说的分割。 我将此更改为:

Book *obj = new Book(onoma, afiksh - 1, meres, arithmos);
解决了这个问题。谢谢你的时间和帮助。
不幸的是,我真的不知道为什么这样做,但它确实做到了,一切正常。因此,我想我必须查看
新的
文档。

摆脱
的所有使用-它们不是必需的,使代码难以阅读,而且您甚至无法一致使用它们。您确定
hotel.getBooks(I)
返回指向有效
书籍的指针吗?@AQUATH无处不在。几乎不需要明确使用<代码> < <代码>。仅仅因为C++程序在特定行上崩溃并不意味着bug在哪里。返回字符串的方式没有问题。这个bug可以在任何地方,这就是为什么需要一个完全可复制的。欢迎来到C++!调试器告诉您的只是程序崩溃的地方。正如我所解释的,这与“bug就在那里”不是一回事。我可以简单地编写一个递归函数,该函数在堆栈上到处乱扔,但返回很好,崩溃将发生在函数调用方的调用方中,在代码的一些不相关部分。这就是它将崩溃的地方,也就是调试器将向您显示该行的地方。但这不是问题所在。你能重现这次事故真是太好了。但是如果你想让别人帮你解决问题,他们也必须这样做。不可能,没有一个。
Book obj(onoma, afiksh - 1, meres, arithmos);
Book *obj = new Book(onoma, afiksh - 1, meres, arithmos);