Groups | Search | Server Info | Login | Register


Groups > gnu.g++.help > #342

Re: Trouble debugging some STL code

Newsgroups gnu.g++.help
Date 2018-02-04 19:23 -0800
References <c23417d4-72c6-4190-9650-06e5e606b745@googlegroups.com> <b80b2102-b7f9-4fad-996a-75a7df1df2fa@googlegroups.com> <877es0k780.fsf@LkoBDZeT.terraraq.uk>
Message-ID <4bf2cfb2-353e-4720-84e2-4fd1e2bd199e@googlegroups.com> (permalink)
Subject Re: Trouble debugging some STL code
From "davin.pearson@gmail.com" <davin.pearson@gmail.com>

Show all headers | View raw


On Tuesday, January 30, 2018 at 4:36:16 AM UTC+13, Richard Kettlewell wrote:
> "davin.pearson" writes:
> 
> > The following code generates a linker error:
> [...]
> 
> You’ve tried to explicitly instantiate a template, but got the syntax
> wrong. See
> https://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html for
> details.
> 
> -- 
> https://www.greenend.org.uk/rjk/

Thank you for your helpful posting.

I've got another problem for you.

Here is my offending file:

#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <string>
#include "../../2006/libd/string.hh"

class Writer
{
public:
   friend Writer& operator << (Writer& w, const std::string& str)
   {
      w << str.c_str();
      return w;
   }
   Writer& operator << (const dmp::string& str)
   {
      *this << str.const_char_star();
      return *this;
   }
   Writer& operator << (const char* s)
   {
      for (const char* ch = s; *ch != 0; ++ch)
      {
         printf("%c",*ch);
      }
      return *this;
   }
   Writer& operator << (const int& i)
   {
      printf("%d",i);
      return *this;
   }
   Writer& operator << (char ch)
   {
      printf("%c",ch);
      return *this;
   }
};

const char endl = '\n';

class File_Writer : public Writer
{
private:
   FILE* f;

public:
   File_Writer(FILE* f)
   {
      this->f = f;
   }

};

File_Writer cout(stdout);

template<typename K>
Writer& operator << (Writer& w, const std::set<K>& set)
{
   typename std::set<K>::const_iterator it = set.begin();
   while (it != set.end())
   {
      w << *it << "\n";
      it++;
   }
   return w;
}

template Writer& operator << (Writer& w, const std::set<std::string>& set);

int main()
{
   std::set<std::string> myset;
   myset.insert("eggplant");
   myset.insert("xylophone");
   myset.insert("apple");
   myset.insert("carrot");
   myset.insert("banana");
   {
      std::set<std::string>::iterator it = myset.find("banana");
      if (it == myset.end())
      {
         cout << "Banana not found" << endl;
         //std::cout << endl << *it; // SEGV:
         //*it = "Fourth"; // NOT ALLOWED
      }
      else
      {
         cout << "Banana found\n";
         cout << "it=" << *it << endl;
      }
   }
   {
      std::set<std::string>::iterator it = myset.find("xbanana");
      if (it == myset.end())
      {
         cout << "xbanana not found" << endl;
         //std::cout << endl << *it; // SEGV:
         //*it = "Fourth"; // NOT ALLOWED
      }
      else
      {
         cout << "xbanana found\n";
         cout << "it=" << *it << endl;
      }
   }
   cout << "myset=" << myset << "\n";
   return 0;
}


cd 2018/map/ && ./set6.exe
Banana found
xbanana not found
myset=apple
banana
carrot
eggplant
xylophone

Here is the contents of the dmp::string class:

http://davinpearson.com/binaries/string.hh

With dmp::string in place of std::string it returns the following
printout:

cd 2018/map/ && ./set6.exe
Banana not found
xbanana not found
myset=apple
banana
carrot
eggplant
xylophone

Why is it returning banana not found? (it is in the set).

Back to gnu.g++.help | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Trouble debugging some STL code "davin.pearson@gmail.com" <davin.pearson@gmail.com> - 2018-01-24 21:47 -0800
  Re: Trouble debugging some STL code Richard Kettlewell <invalid@invalid.invalid> - 2018-01-25 19:52 +0000
    Re: Trouble debugging some STL code "davin.pearson@gmail.com" <davin.pearson@gmail.com> - 2018-01-25 19:04 -0800
  Re: Trouble debugging some STL code "davin.pearson@gmail.com" <davin.pearson@gmail.com> - 2018-01-28 19:12 -0800
    Re: Trouble debugging some STL code Richard Kettlewell <invalid@invalid.invalid> - 2018-01-29 15:36 +0000
      Re: Trouble debugging some STL code "davin.pearson@gmail.com" <davin.pearson@gmail.com> - 2018-02-04 19:23 -0800
        Re: Trouble debugging some STL code Richard Kettlewell <invalid@invalid.invalid> - 2018-02-05 08:26 +0000

csiph-web