1. 程式人生 > >平日小問題 (2):[Error] 'setprecision' is not a member of 'std'

平日小問題 (2):[Error] 'setprecision' is not a member of 'std'

#include <iostream>

template <typename t>
t sum(t x,t y)
{
    return x+y;
}

int main()
{
    int a,b;
    double c,d;

    std::cin>>a>>b>>c>>d;

    std::cout<<sum(a,b)<<std::endl;
    std::cout<<std::fixed<<std::setprecision(3)<<sum(c,d)<<std:
:endl; }

報錯:[Error] ‘setprecision’ is not a member of ‘std’

需要加上標頭檔案 便可通過編譯

修改後:

#include <iostream>
#include <iomanip>
template <typename t>
t sum(t x,t y)
{
    return x+y;
}

int main()
{
    int a,b;
    double c,d;

    std::cin>>a>>b>>c>>d;

    std:
:cout<<sum(a,b)<<std::endl; std::cout<<std::fixed<<std::setprecision(3)<<sum(c,d)<<std::endl; }