0

I'm having a problem with the following code:

#ifndef UTILS_H
#define UTILS_H
#include <cstdint>
#include <iostream>


namespace Utils
{
    template<class T> 
    std::string hexify(T val)
    {
        std::stringstream stream;
        stream << "0x"
            << std::setfill('0') << std::setw(sizeof(T) * 2)
            << std::hex << val;
        return stream.str();
    }
};

#endif

It tells me that "'setw': identifier not found", and the same for every std:: function call.

What's wrong with it?

user66875
  • 2,772
  • 3
  • 29
  • 55

1 Answers1

3

Set field width (setw) belongs to <iomanip> which you've not included in your code apparently. Try this code :

#ifndef UTILS_H
#define UTILS_H
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>     

namespace Utils
{
    template<class T> 
    std::string hexify(T val)
    {
        std::stringstream stream;
        stream << "0x"
            << std::setfill('0') << std::setw(sizeof(T) * 2)
            << std::hex << val;
        return stream.str();
    }
};

#endif

for more info : http://www.cplusplus.com/reference/iomanip/setw/

Adeel Ahmad
  • 990
  • 8
  • 22
  • `stringstream` also needs an include. – Konrad Rudolph Sep 17 '15 at 12:12
  • 1
    Also, sometimes cppreference.com is more accurate than cplusplus.com ... –  Sep 17 '15 at 12:14
  • 1
    @KonradRudolph check this http://stackoverflow.com/questions/9539650/why-does-omission-of-include-string-only-sometimes-cause-compilation-failur – Adeel Ahmad Sep 17 '15 at 12:20
  • 1
    @ÅdəəlÅhmåd What’s your point? The code is invalid. It may happen to work but it’s invalid. Incidentally, the linked question was answered *by me*, and you should read that answer. – Konrad Rudolph Sep 17 '15 at 12:21
  • If your top most comment is a suggestion , then yup i agree, but this chunk of code is working so inclusion of stringstream is not necessary. that is my point. still respect your opinion so editing my answer . – Adeel Ahmad Sep 17 '15 at 12:23
  • 1
    It isn't a matter of opinion. Without the necessary includes your code can be considered to be broken. – juanchopanza Sep 17 '15 at 12:26