I want to run the following code. Its giving error as "Fatal Error" not able to open 'print.hpp' . No such file or directory.. I didn't understand what is #include "print.hpp". what type of file it is. shall we have to write this header file before executing the program.

// stl/transform1.cpp

   #include <iostream>
   #include <vector>
   #include <set>
   #include <algorithm>
   #include "print.hpp"
   #include<conio.h>

   int square (int value)
   {
       return value*value;
   }

   int main() 
   {
       std::set<int> coll1;
       std::vector<int> coll2;

       //insert elements from 1 to 9 into coll1
       for (int i=1; i<=9; ++i) {
           coll1.insert(i);
       }
       PRINT_ELEMENTS(coll1,"initialized: ");

       //transform each element from coll1 to coll2
       // - square transformed values
       std::transform (coll1.begin(),coll1.end(),     //source
                       std::back_inserter(coll2),     //destination
                       square);                       //operation

       PRINT_ELEMENTS(coll2,"squared:        ");
   getch();
   }

Dani AI

Generated

A short, practical note for the thread: the compiler error "fatal error: print.hpp: No such file or directory" means the preprocessor could not locate that header. The form #include "print.hpp" tells the implementation to search the source file’s directory (then the usual include paths); either place a local file named print.hpp next to the .cpp, add its directory to the compiler include path (e.g. -I), or remove the include and put the helper directly into the translation unit. As suggested, creating the header fixes the immediate error; as noted, that sample helper is not part of the standard library. (cppreference.com)

Why the header must usually contain the full template: helper functions implemented as templates must be defined where they are instantiated, so templates are normally provided in headers rather than a separate compiled .o file. Protect the header with a simple include guard or #pragma once to avoid multiple-inclusion issues. Providing the complete template definition in the header (or in an inline header-only helper) avoids linkage/instantiation surprises. (cppref.microblock.cc)

A cleaner alternative to a custom PRINT_ELEMENTS helper is to leverage standard iterators and algorithms. For printing, std::copy with std::ostream_iterator or a range-based for loop removes the need for a bespoke printer; for transformations the existing std::transform + std::back_inserter pattern is idiomatic and correct. Example (print with iterator):

#include <iostream>
#include <iterator>
#include <algorithm>

std::copy(coll.begin(), coll.end(),
          std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';

See std::ostream_iterator, std::transform and std::back_inserter for details. (cppreference.net)

One final note: conio.h (and its getch style helpers) is nonstandard and present only on some compilers/OSes; prefer standard input functions or platform-specific alternatives when portability is required. For doing arithmetic/relational/logical work with the STL, the chapters covering <algorithm>, <numeric> and function objects (or modern lambdas) — the algorithms std::transform, std::accumulate, std::for_each, std::count_if, etc. — are the most relevant. (en.wikipedia.org)

Recommended Answers

All 6 Replies

Let me guess: This came out of: The C++ Standard Library - A Tutorial and Reference?

if yes:
create a file "print.hpp" and wack this code in it:

#include <iostream>

template <class T>
inline void print_elements (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}

Or just throw the code in your cpp file and delete the #include "print.hpp" Also loose the getch() . If you want to play with c++ use cin.get() instead. That way you can also delete the line #include <conio.h> . The conio header isn't standard c++

>> shall we have to write this header file before executing the program.
Yes, or get it from whereever you got that *.cpp file. The file you posted is not part of STL because STL doesn't contain main().

Let me guess: This came out of: The C++ Standard Library - A Tutorial and Reference?

if yes:
create a file "print.hpp" and wack this code in it:

#include <iostream>

template <class T>
inline void print_elements (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}

Or just throw the code in your cpp file and delete the #include "print.hpp" Also loose the getch() . If you want to play with c++ use cin.get() instead. That way you can also delete the line #include <conio.h> . The conio header isn't standard c++

Thanks for quick reply. I got output..

Let me guess: This came out of: The C++ Standard Library - A Tutorial and Reference?

if yes:
create a file "print.hpp" and wack this code in it:

#include <iostream>

template <class T>
inline void print_elements (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}

Or just throw the code in your cpp file and delete the #include "print.hpp" Also loose the getch() . If you want to play with c++ use cin.get() instead. That way you can also delete the line #include <conio.h> . The conio header isn't standard c++

yes i am reading The C++ Standard Library - A Tutorial and Reference. I want to the program on aritmetic/relational/logical (etc) operations using STLs. In the above said book Which topics will help me to do these programs. I completed 5th chapter in this book. See attach for complete program..

Did you read the link I posted in your other thread?

The attachment looks to me like a homework assignment, so you'll have to try it yourself. Come back when you have a specific problem with a piece of code.

[edit]
And as my signature says: DO NOT PM me for help.

Did you read the link I posted in your other thread?

The attachment looks to me like a homework assignment, so you'll have to try it yourself. Come back when you have a specific problem with a piece of code.

[edit]
And as my signature says: DO NOT PM me for help.

I am very very sorry. I have not yet read link which you posted in previous thread. I am reading c++ The standard library A tutorial and reference. After completing this tutorial i will go to read that link. and will get back to you if any problem occurs in my code.
I am very much thankful to you for your guideness

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.