0

I have the following header file. This compiled fine in VS2010 and VS2013, but now fails in VS2015

#ifndef UTILITY_IOS_STATE_H
#define UTILITY_IOS_STATE_H

#include <iosfwd>

template <class Value>
struct SetPrecision
{
    SetPrecision(int precision, const Value& value) : _precision(precision), _value(value) {}

    const int _precision;
    const Value _value;
};

template <class Value>
inline SetPrecision<Value> precision(int precision, const Value& value) {
    return SetPrecision<Value>(precision, value);
}

template <class Value>
std::ostream& operator<<(std::ostream& out, const SetPrecision<Value>& p)
{
    std::streamsize precision = out.precision();
    out.precision(p._precision);
    out << p._value;
    out.precision(precision);
    return out;
}

template <class Value>
struct SetFixedPrecision
{
    SetFixedPrecision(int precision, const Value& value) : _precision(precision), _value(value) {}

    const int _precision;
    const Value _value;
};

template <class Value>
inline SetFixedPrecision<Value> fixedPrecision(int precision, const Value& value) {
    return SetFixedPrecision<Value>(precision, value);
}

template <class Value>
std::ostream& operator<<(std::ostream& out, const SetFixedPrecision<Value>& p)
{
    std::streamsize precision = out.precision();
    std::ios_base::fmtflags flags = out.flags();

    out.precision(p._precision);
    out.setf(ios_base::fixed, ios_base::floatfield);

    out << p._value;

    out.flags(flags);
    out.precision(precision);

    return out;
}

#endif  /* UTILITY_IOS_STATE_H */

the offending line is:

out.setf(ios_base::fixed, ios_base::floatfield);

The compiler errors are all about the class ios_base in "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xiosbase" (this used to be "c:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xiosbase"). The errors are all

Severity Code Description Project File Line Suppression State Error C2065 'floatfield': undeclared identifier MyProject F:\MyProject\src\utility\ios_state.h 51

Severity Code Description Project File Line Suppression State Error C2653 'ios_base': is not a class or namespace name MyProject F:\MyProject\src\utility\ios_state.h 51

and 21 others on the same line all complaining about ios_base being an undeclared identifier.

I am confused over why this has appeared, is this due to ambiguity in the references? If not, why am I getting this error and how can I fix it?

Thanks for your time.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277

2 Answers2

1

It needs to be std::ios_base, like the other ios_base references in the function.

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
  • OMG, it has been a long day, a Ctrl+Z shows me I accidentally deleted it!! Idiot I am. Thanks very much for your time – MoonKnight Dec 07 '15 at 18:57
0

You can't include <iosfwd> only and than use streams. The code as posted should not compile at all. <iosfwd> is only for references to stream classess, and does not support inserters invocation. For your code, you should be including <iostream>.

SergeyA
  • 61,605
  • 5
  • 78
  • 137