Initializing a smart pointer to an instance of class and accessing its methods

Brianna Schmidt

So, I'm trying to implement a priority queue using a sorted list. I have my priority queue class inherit from my sorted list class. My sorted list class works fine when I test its functions in the driver, but I am getting "Segmentation Fault 11" when I try testing out my implementation of the priority queue. I ran my code through the CLion debugger and got the error "EXC_BAD_ACCESS (code=1, address=0x0)", which, after some research, seems to be caused by trying to modify data in a null pointer. This is my first time using smart pointers, so I think my issues lie in a misunderstanding with how they're constructed and initialized.

Here's my header file for my SL_PriorityQueue class:

#ifndef PRIORITY_QUEUE_
#define PRIORITY_QUEUE_

#include "PriorityQueueInterface.h"
#include "LinkedSortedList.h"
#include "PrecondViolatedExcep.h"
#include <memory>

template<class ItemType>
class SL_PriorityQueue : public PriorityQueueInterface<ItemType>
{
  private:
     std::unique_ptr<LinkedSortedList<ItemType> > slistPtr;

  public:
    SL_PriorityQueue();
    SL_PriorityQueue(const SL_PriorityQueue& pq);
    ~SL_PriorityQueue();

    bool isEmpty() const;
    bool enqueue(const ItemType& newEntry);
    bool dequeue();

    //@throw PrecondViolatedExcept if priority queue is isEmpty
    ItemType peekFront() const throw(PrecondViolatedExcep);
};

#endif

Here's the driver I'm using to test my code:

#include "../src/Node.cpp"
#include "../src/LinkedSortedList.cpp"
#include "../src/SL_PriorityQueue.cpp"
#include <iostream>

int main()
{
    std::shared_ptr<SL_PriorityQueue<int> > testing (new SL_PriorityQueue<int>());
    testing->enqueue(7);
    std::cout << testing->peekFront() << std::endl; //I set a break point here, which is where CLion throws the exception
    std::cout << testing->dequeue() << std::endl;
    std::cout << testing->isEmpty() << std::endl;
    return 0;
}

Here's the function from SL_PriorityQueue.cpp that the CLion highlighted after the exception:

template <class ItemType>
bool SL_PriorityQueue<ItemType>::enqueue(const ItemType& newEntry)
{
  slistPtr->insertSorted(newEntry);
  return true;
}

The error makes me think that when I call insertSorted in the above function, slistPtr is still null. My constructor for SL_PriorityQueue is empty since it seems that with smart pointers they manage their own memory such that I don't have to set it equal to a null pointer.

I tried making slistPtr a shared pointer to see if maybe too many things were pointing to it, but I just received the same error.

Thanks in advance for any help you can provide!

AzCopey

My constructor for SL_PriorityQueue is empty since it seems that with smart pointers they manage their own memory such that I don't have to set it equal to a null pointer.

If I've understood you correctly, you've got a bit mixed up here. Although a unique_ptr will correctly clean up memory it owns, it won't allocate anything itself. The best way of doing so is to use std::make_unique, which you can call from your constructor.

template <typename ItemType>
SL_PriorityQueue<ItemType>::SL_PriorityQueue()
   : slistPtr(std::make_unique<LinkedSortedList<ItemType>>())
{
}

Hope that helps!

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

c++. Smart pointer for a member object of a class whose instance itself is owned by a smart pointer. Necessary?

分類Dev

What is the reason behind a class holding a pointer to its instance as a private member?

分類Dev

How to dereference pointer to a class instance?

分類Dev

Accessing static members of a class within static methods

分類Dev

Saving / accessing fields from Class methods (Django)

分類Dev

Create a new instance of a class, based on its "Class"

分類Dev

Perform list of class methods on instance of class

分類Dev

Accessing derived class-only methods using base class object

分類Dev

Get a pointer pointer for a smart pointer

分類Dev

Initialize a new instance of the class through its method

分類Dev

Delete an instance from its class' dict in desctructor?

分類Dev

Default initializing a pointer in a template

分類Dev

Initializing nested void pointer

分類Dev

Initializing a pointer to an array in C

分類Dev

Accessing pointer with pointer to a literal

分類Dev

Why can instance methods be called as class methods in Python 3?

分類Dev

Initializing an extended class with an instance when the parent class can't take itself as argument for the constructor

分類Dev

Using a getter with a smart pointer

分類Dev

How to fix undefined File() class and its methods in Dart?

分類Dev

How to get a Geb module instance with its declared class?

分類Dev

Mutating objects referenced by class attributes through instance methods

分類Dev

Accessing to the zero position with a pointer

分類Dev

How can I use child class methods not defined in a parent class from a parent class pointer?

分類Dev

Initializing member class with derived class

分類Dev

In Java, initializing and accessing array inside a method?

分類Dev

multiple threads accessing methods

分類Dev

Smart Pointer casting in Boost::Python

分類Dev

Why doesn't clang allow accessing a nested enum class through an instance?

分類Dev

Why are there two different ways of initializing a pointer in C

Related 関連記事

  1. 1

    c++. Smart pointer for a member object of a class whose instance itself is owned by a smart pointer. Necessary?

  2. 2

    What is the reason behind a class holding a pointer to its instance as a private member?

  3. 3

    How to dereference pointer to a class instance?

  4. 4

    Accessing static members of a class within static methods

  5. 5

    Saving / accessing fields from Class methods (Django)

  6. 6

    Create a new instance of a class, based on its "Class"

  7. 7

    Perform list of class methods on instance of class

  8. 8

    Accessing derived class-only methods using base class object

  9. 9

    Get a pointer pointer for a smart pointer

  10. 10

    Initialize a new instance of the class through its method

  11. 11

    Delete an instance from its class' dict in desctructor?

  12. 12

    Default initializing a pointer in a template

  13. 13

    Initializing nested void pointer

  14. 14

    Initializing a pointer to an array in C

  15. 15

    Accessing pointer with pointer to a literal

  16. 16

    Why can instance methods be called as class methods in Python 3?

  17. 17

    Initializing an extended class with an instance when the parent class can't take itself as argument for the constructor

  18. 18

    Using a getter with a smart pointer

  19. 19

    How to fix undefined File() class and its methods in Dart?

  20. 20

    How to get a Geb module instance with its declared class?

  21. 21

    Mutating objects referenced by class attributes through instance methods

  22. 22

    Accessing to the zero position with a pointer

  23. 23

    How can I use child class methods not defined in a parent class from a parent class pointer?

  24. 24

    Initializing member class with derived class

  25. 25

    In Java, initializing and accessing array inside a method?

  26. 26

    multiple threads accessing methods

  27. 27

    Smart Pointer casting in Boost::Python

  28. 28

    Why doesn't clang allow accessing a nested enum class through an instance?

  29. 29

    Why are there two different ways of initializing a pointer in C

ホットタグ

アーカイブ