Trouble debugging some STL code

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Trouble debugging some STL code

davin.pearson@gmail.com
#include <iostream>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <string>

std::ostream& operator << (std::ostream& o, const std::pair<std::string, int> p)
{
   o << p.first << " " << p.second << std::endl;
   return o;
}

std::ostream& operator << (std::ostream& o, const std::map<std::string,int>& map)
{
   typename std::map<std::string,int>::iterator it = map.begin();
   o << "(";
   while (it != map.end())
   {
      o << *it << ' ';
      ++it;
   }
   o << ")";
   return o;
}

int main()
{
   std::map<std::string,int> map;
   std::cout << "map=" << map << std::endl;
}


-----------------------------------------------------------------------------

Here is the compiler output:

-*- mode: compilation; default-directory: "~/lisp++-projects/" -*-
Compilation started at Thu Jan 25 18:45:22

cd ~/lisp++-projects && make 2018/map/map5.run
make: Warning: File '2006/libd/libd.o' has modification time 26862 s in the future
* Compiling 2018/map/map5.cc DEBUG
g++ -g -O0 -std=c++11 -W -Wall -Wpointer-arith -Wformat -ffast-math -fno-permissive -Wno-format-contains-nul -c 2018/map/map5.cc -o 2018/map/map5.o
2018/map/map5.cc: In function ‘std::ostream& operator<<(std::ostream&, const std::map<std::__cxx11::basic_string<char>, int>&)’:
2018/map/map5.cc:16:63: error: conversion from ‘std::map<std::__cxx11::basic_string<char>, int>::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const std::__cxx11::basic_string<char>, int> >}’ to non-scalar type ‘std::map<std::__cxx11::basic_string<char>, int>::iterator {aka std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, int> >}’ requested
    typename std::map<std::string,int>::iterator it = map.begin();
                                                               ^
Makefile.mk:66: recipe for target '2018/map/map5.o' failed
make: *** [2018/map/map5.o] Error 1

Compilation exited abnormally with code 2 at Thu Jan 25 18:45:24
Compilation started at: 20180125-184522
compilation-start-time=(23145 28402 353882 710000)
compilation-stop-time=(23145 28404 423781 491000)
Compilation took 2 seconds

What gives with the error message?  What mm I doing wrong?
_______________________________________________
help-gplusplus mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus
Reply | Threaded
Open this post in threaded view
|

Re: Trouble debugging some STL code

Richard Kettlewell-2
"[hidden email]" <[hidden email]> writes:
> std::ostream& operator << (std::ostream& o, const std::map<std::string,int>& map)
> {
>    typename std::map<std::string,int>::iterator it = map.begin();

[...]

> cd ~/lisp++-projects && make 2018/map/map5.run
> make: Warning: File '2006/libd/libd.o' has modification time 26862 s in the future
> * Compiling 2018/map/map5.cc DEBUG
> g++ -g -O0 -std=c++11 -W -Wall -Wpointer-arith -Wformat -ffast-math -fno-permissive -Wno-format-contains-nul -c 2018/map/map5.cc -o 2018/map/map5.o
> 2018/map/map5.cc: In function ‘std::ostream& operator<<(std::ostream&, const std::map<std::__cxx11::basic_string<char>, int>&)’:
> 2018/map/map5.cc:16:63: error: conversion from ‘std::map<std::__cxx11::basic_string<char>, int>::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const std::__cxx11::basic_string<char>, int> >}’ to non-scalar type ‘std::map<std::__cxx11::basic_string<char>, int>::iterator {aka std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, int> >}’ requested
>     typename std::map<std::string,int>::iterator it = map.begin();
>                                                                ^
> Makefile.mk:66: recipe for target '2018/map/map5.o' failed
> make: *** [2018/map/map5.o] Error 1
>
> Compilation exited abnormally with code 2 at Thu Jan 25 18:45:24
> Compilation started at: 20180125-184522
> compilation-start-time=(23145 28402 353882 710000)
> compilation-stop-time=(23145 28404 423781 491000)
> Compilation took 2 seconds
>
> What gives with the error message?  What mm I doing wrong?

You’ve got a const_iterator, you’re trying to convert it to an
iterator.

At a higher level, arguably what you’re doing wrong is typing out
complete (incorrect) type names instead of using ‘auto’.

--
https://www.greenend.org.uk/rjk/
_______________________________________________
help-gplusplus mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus
Reply | Threaded
Open this post in threaded view
|

Re: Trouble debugging some STL code

davin.pearson@gmail.com
Thank you for taking the time to fix my bug.  It becomes obvious when the solution is presented to me.

On Friday, January 26, 2018 at 8:52:06 AM UTC+13, Richard Kettlewell wrote:

> writes:
> > std::ostream& operator << (std::ostream& o, const std::map<std::string,int>& map)
> > {
> >    typename std::map<std::string,int>::iterator it = map.begin();
>
> [...]
>
> > cd ~/lisp++-projects && make 2018/map/map5.run
> > make: Warning: File '2006/libd/libd.o' has modification time 26862 s in the future
> > * Compiling 2018/map/map5.cc DEBUG
> > g++ -g -O0 -std=c++11 -W -Wall -Wpointer-arith -Wformat -ffast-math -fno-permissive -Wno-format-contains-nul -c 2018/map/map5.cc -o 2018/map/map5.o
> > 2018/map/map5.cc: In function ‘std::ostream& operator<<(std::ostream&, const std::map<std::__cxx11::basic_string<char>, int>&)’:
> > 2018/map/map5.cc:16:63: error: conversion from ‘std::map<std::__cxx11::basic_string<char>, int>::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const std::__cxx11::basic_string<char>, int> >}’ to non-scalar type ‘std::map<std::__cxx11::basic_string<char>, int>::iterator {aka std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, int> >}’ requested
> >     typename std::map<std::string,int>::iterator it = map.begin();
> >                                                                ^
> > Makefile.mk:66: recipe for target '2018/map/map5.o' failed
> > make: *** [2018/map/map5.o] Error 1
> >
> > Compilation exited abnormally with code 2 at Thu Jan 25 18:45:24
> > Compilation started at: 20180125-184522
> > compilation-start-time=(23145 28402 353882 710000)
> > compilation-stop-time=(23145 28404 423781 491000)
> > Compilation took 2 seconds
> >
> > What gives with the error message?  What mm I doing wrong?
>
> You’ve got a const_iterator, you’re trying to convert it to an
> iterator.
>
> At a higher level, arguably what you’re doing wrong is typing out
> complete (incorrect) type names instead of using ‘auto’.
>
> --
> https://www.greenend.org.uk/rjk/

_______________________________________________
help-gplusplus mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus
Reply | Threaded
Open this post in threaded view
|

Re: Trouble debugging some STL code

davin.pearson@gmail.com
In reply to this post by davin.pearson@gmail.com
The following code generates a linker error:

#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <string>

class Writer
{
public:

   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;
   }
};
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;
}

Writer& operator << (Writer& w, const std::string& str)
{
   w << str.c_str();
   return w;
}

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"; // buggy line of code
   return 0;
}


Here is the output from invoking G++ on the above code:

-*- mode: compilation; default-directory: "~/lisp++-projects/" -*-
Compilation started at Tue Jan 30 04:05:24

cd ~/lisp++-projects && make 2018/map/set2.run
* Compiling 2018/map/set2.cc DEBUG
g++ -g -O0 -std=c++11 -W -Wall -Wpointer-arith -Wformat -ffast-math -fno-permissive -Wno-format-contains-nul -c 2018/map/set2.cc -o 2018/map/set2.o
** Linking 2018/map/set2.exe DEBUG
g++  -lgcc -lstdc++  2018/map/set2.o 2006/libd/libd.o -o 2018/map/set2.exe
2018/map/set2.o: In function `main':
/home/www/lisp++-projects/2018/map/set2.cc:100: undefined reference to `operator<<(Writer&, std::set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)'
collect2: error: ld returned 1 exit status
Makefile.mk:86: recipe for target '2018/map/set2.exe' failed
make: *** [2018/map/set2.exe] Error 1

Compilation exited abnormally with code 2 at Tue Jan 30 04:05:27
Compilation started at: 20180130-040524
compilation-start-time=(23151 14388 436671 507000)
compilation-stop-time=(23151 14391 819805 265000)
Compilation took 3 seconds
_______________________________________________
help-gplusplus mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus
Reply | Threaded
Open this post in threaded view
|

Re: Trouble debugging some STL code

Richard Kettlewell-2
"[hidden email]" <[hidden email]> 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/
_______________________________________________
help-gplusplus mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus
Reply | Threaded
Open this post in threaded view
|

Re: Trouble debugging some STL code

davin.pearson@gmail.com
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
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus
Reply | Threaded
Open this post in threaded view
|

Re: Trouble debugging some STL code

Richard Kettlewell-2
"[hidden email]" <[hidden email]> writes:
> http://davinpearson.com/binaries/string.hh

Your operator< overloads in dmp::string are broken. Possibly other bugs,
I didn’t look any further.

--
https://www.greenend.org.uk/rjk/
_______________________________________________
help-gplusplus mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-gplusplus