Home > Back-end >  Compact String Set with smart pointers
Compact String Set with smart pointers

Time:05-20

The project was to make our own set class that we went over, but to use smart pointers. I got all my functions to work without smart pointer, but now that I tried to use them I'm getting issues with creating a new node.

#include "cs19_compact_string_set.h"
#include <memory>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#include <string>

namespace cs19 {

CompactStringSet::CompactStringSet() :root_{0}, num_strings_{0} {}
bool CompactStringSet::insert(const std::string& value) {
  if (this->find(value)) return true;
  auto cur = this->root_;                       
  for (auto character : value) {            
    auto search = this->find_next(cur, character); 
    if (search) {                              
      cur = search;                                  
    } else {                                      
      auto new_node = std::shared_ptr<CompactStringSet::Node>(character);
      if (cur->child) {    
        cur = cur->child;  
        while (cur->sibling)
          cur = cur->sibling;    
        cur->sibling = new_node; 
      } else {
        cur->child = new_node; 
      }
      cur = new_node;
    }
  }
  if (!cur->terminal) {   
      this->num_strings_;  
    cur->terminal = true;  
  }
  return false;
}
std::shared_ptr<CompactStringSet::Node> CompactStringSet::find_next(const 
std::shared_ptr<CompactStringSet::Node> base, char to_find) const {   
  if (base->child) {
    if (base->child->letter == to_find)
      return base->child;
    auto sibling = base->child->sibling;
    while (sibling) {
      if (sibling->letter == to_find)
        return sibling;
      sibling = sibling->sibling;
    }
  }
  return nullptr;  // No match found
}

}  // namespace cs19

In the file where the functions are being implemented I keep getting errors for trying to make a new shared_ptr with the value character. I've tried to change it in a few different ways. but can't solve the issue. The error keeps reading error: no matching function for call to ‘std::shared_ptrcs19::CompactStringSet::Node::shared_ptr(char&)’.

#ifndef CS19_COMPACT_STRING_SET_H_
#define CS19_COMPACT_STRING_SET_H_

#include <memory>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#include <string>

namespace cs19 {

class CompactStringSet {

  struct Node {
    char letter;              // each node stores a letter
    bool terminal = false;    // ... and is potentially the end of a string in the set
    std::shared_ptr<Node> sibling = nullptr;
    std::shared_ptr<Node> child = nullptr;
  };

 public:
  CompactStringSet();
  bool insert(const std::string& value);
  bool find(const std::string& value) const;
  bool end() const {
    return false;
  }
  std::size_t size() const {
   return this->num_strings_;
  }  
 private:
  std::shared_ptr<Node> root_{0};   
  std::size_t num_strings_ = 0;
  std::shared_ptr<Node> find_next(const std::shared_ptr<Node> base, char to_find) const;};

}  // namespace cs19

#endif

CodePudding user response:

You are trying to create a new instance of CompactStringSet by giving it a character as a parameter in the constructor

   auto new_node = std::shared_ptr<CompactStringSet::Node>(character);

But your structure doesn't take any parameters and doesn't have any constructor, it's what the error says.

So you should propably replace it by :

   auto new_node = std::make_shared<CompactStringSet::Node>();
   new_node->letter = character;

You should also use make_shared instead of shared_ptr when you want to create a new shared_ptr

CodePudding user response:

You code fails because you are trying to invoke a non existent function here

 auto new_node = std::shared_ptr<CompactStringSet::Node>(character);

Seems like you somehow expect this to create a new node, assign the character to its 'letter' member and create a shared_ptr pointing at the new node. This is wishful thinking, no such function exisits - hence

no matching function for call to ‘std::shared_ptrcs19::CompactStringSet::Node::shared_ptr(char&)’.

You need (since you have no constructor for 'node' that accepts a character argument)

 auto new_nodew = std::make_shared<CompactStringSet::Node>();
 new_node->letter = character; 
  • Related