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).
_______________________________________________
help-gplusplus mailing list
help-gplusplus@gnu.org
https://lists.gnu.org/mailman/listinfo/help-gplusplus

Reply via email to