Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trouble debugging some STL code

16 views
Skip to first unread message

davin....@gmail.com

unread,
Jan 25, 2018, 12:47:21 AM1/25/18
to
#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?

Richard Kettlewell

unread,
Jan 25, 2018, 2:52:06 PM1/25/18
to
"davin....@gmail.com" <davin....@gmail.com> 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/

davin....@gmail.com

unread,
Jan 25, 2018, 10:04:08 PM1/25/18
to
Thank you for taking the time to fix my bug. It becomes obvious when the solution is presented to me.

davin....@gmail.com

unread,
Jan 28, 2018, 10:12:09 PM1/28/18
to
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

Richard Kettlewell

unread,
Jan 29, 2018, 10:36:16 AM1/29/18
to
"davin....@gmail.com" <davin....@gmail.com> 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/

davin....@gmail.com

unread,
Feb 4, 2018, 10:23:16 PM2/4/18
to
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).

Richard Kettlewell

unread,
Feb 5, 2018, 3:26:21 AM2/5/18
to
"davin....@gmail.com" <davin....@gmail.com> 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/
0 new messages