Author Topic: Lots of errors and warnings in std_lib_facilities.h on compile time  (Read 207 times)

Offline yolo_ninja

  • Single posting newcomer
  • *
  • Posts: 5
Hello,
I am using code blocks version 17.12 on windows 10 and I'm using the GNU GCC compiler.

I copied code for a header file called std_lib_facilities out of this link- http://www.stroustrup.com/Programming/std_lib_facilities.h that is required for the progression in the book 'programming principles and practice using c++' and pasted it in a notepad file and saved it in the project folder. I created a project and then added the header file using the add file option. I included std_lib_facilities and compiled the code.

Code: [Select]
#include "std_lib_facilities.h"

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}

std_lib_facilities-
Code: [Select]
#ifndef H112
#define H112 251113L


#include<iostream>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include <forward_list>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include <array>
#include <regex>
#include<random>
#include<stdexcept>

//------------------------------------------------------------------------------


//------------------------------------------------------------------------------

typedef long Unicode;

//------------------------------------------------------------------------------

using namespace std;

template<class T> string to_string(const T& t)
{
ostringstream os;
os << t;
return os.str();
}

struct Range_error : out_of_range { // enhanced vector range error reporting
int index;
Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
};


// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
using size_type = typename std::vector<T>::size_type;

#ifdef _MSC_VER
// microsoft doesn't yet support C++11 inheriting constructors
Vector() { }
explicit Vector(size_type n) :std::vector<T>(n) {}
Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
template <class I>
Vector(I first, I last) : std::vector<T>(first, last) {}
Vector(initializer_list<T> list) : std::vector<T>(list) {}
#else
using std::vector<T>::vector; // inheriting constructor
#endif

T& operator[](unsigned int i) // rather than return at(i);
{
if (i<0||this->size()<=i) throw Range_error(i);
return std::vector<T>::operator[](i);
}
const T& operator[](unsigned int i) const
{
if (i<0||this->size()<=i) throw Range_error(i);
return std::vector<T>::operator[](i);
}
};

// disgusting macro hack to get a range checked vector:
#define vector Vector

// trivially range-checked string (no iterator checking):
struct String : std::string {
using size_type = std::string::size_type;
// using string::string;

char& operator[](unsigned int i) // rather than return at(i);
{
if (i<0||size()<=i) throw Range_error(i);
return std::string::operator[](i);
}

const char& operator[](unsigned int i) const
{
if (i<0||size()<=i) throw Range_error(i);
return std::string::operator[](i);
}
};


namespace std {

    template<> struct hash<String>
    {
        size_t operator()(const String& s) const
        {
            return hash<std::string>()(s);
        }
    };

} // of namespace std


struct Exit : runtime_error {
Exit(): runtime_error("Exit") {}
};

// error() simply disguises throws:
inline void error(const string& s)
{
throw runtime_error(s);
}

inline void error(const string& s, const string& s2)
{
error(s+s2);
}

inline void error(const string& s, int i)
{
ostringstream os;
os << s <<": " << i;
error(os.str());
}


template<class T> char* as_bytes(T& i) // needed for binary I/O
{
void* addr = &i; // get the address of the first byte
// of memory used to store the object
return static_cast<char*>(addr); // treat that memory as bytes
}


inline void keep_window_open()
{
cin.clear();
cout << "Please enter a character to exit\n";
char ch;
cin >> ch;
return;
}

inline void keep_window_open(string s)
{
if (s=="") return;
cin.clear();
cin.ignore(120,'\n');
for (;;) {
cout << "Please enter " << s << " to exit\n";
string ss;
while (cin >> ss && ss!=s)
cout << "Please enter " << s << " to exit\n";
return;
}
}



// error function to be used (only) until error() is introduced in Chapter 5:
inline void simple_error(string s) // write ``error: s and exit program
{
cerr << "error: " << s << '\n';
keep_window_open(); // for some Windows environments
exit(1);
}

// make std::min() and std::max() accessible on systems with antisocial macros:
#undef min
#undef max


// run-time checked narrowing cast (type conversion). See ???.
template<class R, class A> R narrow_cast(const A& a)
{
R r = R(a);
if (A(r)!=a) error(string("info loss"));
return r;
}

// random number generators. See 24.7.



inline int randint(int min, int max) { static default_random_engine ran; return uniform_int_distribution<>{min, max}(ran); }

inline int randint(int max) { return randint(0, max); }

//inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x

// container algorithms. See 21.9.

template<typename C>
using Value_type = typename C::value_type;

template<typename C>
using Iterator = typename C::iterator;

template<typename C>
// requires Container<C>()
void sort(C& c)
{
std::sort(c.begin(), c.end());
}

template<typename C, typename Pred>
// requires Container<C>() && Binary_Predicate<Value_type<C>>()
void sort(C& c, Pred p)
{
std::sort(c.begin(), c.end(), p);
}

template<typename C, typename Val>
// requires Container<C>() && Equality_comparable<C,Val>()
Iterator<C> find(C& c, Val v)
{
return std::find(c.begin(), c.end(), v);
}

template<typename C, typename Pred>
// requires Container<C>() && Predicate<Pred,Value_type<C>>()
Iterator<C> find_if(C& c, Pred p)
{
return std::find_if(c.begin(), c.end(), p);
}

#endif //H112

The program compiles and gives me 35 errors and a few thousand warnings. I had to cut the messages down a bit, but they were all the same warning about std_lib_facilities.h:1:8: warning: null character(s) ignored.
Build log-
Code: [Select]
-------------- Build: Debug in using error without exceptions (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -g -std=c++11  -c "C:\coding\drills, review\using error without exceptions\main.cpp" -o obj\Debug\main.o
mingw32-g++.exe  -o "bin\Debug\using error without exceptions.exe" obj\Debug\main.o   
In file included from C:\coding\drills, review\using error without exceptions\main.cpp:1:0:
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:1:1: error: stray '\377' in program
 # i f n d e f   H 1 1 2
 
 ^
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:1:1: error: stray '\376' in program
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:1:3: error: stray '#' in program
 # i f n d e f   H 1 1 2
 
   ^
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:1:4: warning: null character(s) ignored
 # i f n d e f   H 1 1 2
 
    ^
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:1:6: warning: null character(s) ignored
 # i f n d e f   H 1 1 2
 
      ^
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:1:8: warning: null character(s) ignored
 # i f n d e f   H 1 1 2
 
        ^
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:1:10: warning: null character(s) ignored
 # i f n d e f   H 1 1 2
 
          ^


C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:4: error: invalid preprocessing directive #e
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:5: warning: null character(s) ignored
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:7: warning: null character(s) ignored
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:9: warning: null character(s) ignored
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:11: warning: null character(s) ignored
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:13: warning: null character(s) ignored
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:17: warning: null character(s) ignored
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:19: warning: null character(s) ignored
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:21: warning: null character(s) ignored
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:23: warning: null character(s) ignored
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:25: warning: null character(s) ignored
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:463:27: warning: null character(s) ignored
In file included from C:\coding\drills, review\using error without exceptions\main.cpp:1:0:
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:1:5: error: 'i' does not name a type
 # i f n d e f   H 1 1 2
 
     ^
In file included from C:\coding\drills, review\using error without exceptions\main.cpp:1:0:
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:57:2: error: expected unqualified-id before '/' token
    V e c t o r ( i n i t i a l i z e r _ l i s t < T >   l i s t )   :   s t d : : v e c t o r < T > ( l i s t )   { }
 
  ^
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:65:2: error: 't' does not name a type
      r e t u r n   s t d : : v e c t o r < T > : : o p e r a t o r [ ] ( i ) ;
 
  ^
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:79:2: error: 's' does not name a type
    u s i n g   s i z e _ t y p e   =   s t d : : s t r i n g : : s i z e _ t y p e ;
 
  ^
C:\coding\drills, review\using error without exceptions\std_lib_facilities.h:91:2: error: expected unqualified-id before '/' token
      r e t u r n   s t d : : s t r i n g : : o p e r a t o r [ ] ( i ) ;
 
  ^
Process terminated with status 1 (0 minute(s), 14 second(s))
35 error(s), 4624 warning(s) (0 minute(s), 14 second(s))
 

Thanks.

Toby.
« Last Edit: April 17, 2018, 09:47:10 am by yolo_ninja »


Online BlueHazzard

  • Lives here!
  • ****
  • Posts: 1780
Re: Lots of errors and warnings in std_lib_facilities.h on compile time
« Reply #2 on: April 17, 2018, 10:56:01 am »
your file encoding is messed up...
i am not sure how this is possible...

Open your header file inside codeblocks then edit->File encoding->UTF-8 and save the file...

OR: even more simple: Save the file without modfying it directly from the browser to your project folder.
OR: Copy the file from the explorer and paste it in codeblocks in a new file (file->New) and save it in your project folder...

Offline yolo_ninja

  • Single posting newcomer
  • *
  • Posts: 5
Re: Lots of errors and warnings in std_lib_facilities.h on compile time
« Reply #3 on: April 17, 2018, 11:53:27 am »
Hey thanks a lot, definitely encoding, working fine now.