C++"使用"运算符使标准数据障碍的对象可用

C++ 'using' operator to make object of std available

本文关键字:对象 数据 使用 运算符 标准 C++      更新时间:2023-10-16

请看一下我的代码:

#include <iostream>
using std::cin;  // I would like to make cin object available
using std::cout; // I would like to make cout object available in source code
using std::get;  // I would like to make cin.get() available
using std::fail; // I would like to make cin.fail() available
factorial(int number);
int main()
{
    int closer;
    cin >> closer;
    while (!cin.fail())
    {
        factorial(closer); // it's obvious going by the name, It's factorial function
        cin >> closer;
    }
}
factorial(int number)
{
    // the function is correct and it's been examined before....
}

我在linux中使用gcc,有这个错误:

factor.cpp:8:12: error: ‘std::get’ has not been declared
factor.cpp:9:12: error: ‘std::fail’ has not been declared

是否有错别字或逻辑错误

您不需要使用using作为成员函数,它们可以毫无问题地使用:

#include <iostream>
using std::cin;  // I would like to make cin object available
using std::cout; // I would like to make cout object available in source code
factorial(int number);
int main()
{
    int closer;
    cin >> closer;
    while (!cin.fail()) // fail is part of the cin type and not part of the std namespace
    {
        factorial(closer); // it's obvious going by the name, It's factorial function
        cin >> closer;
    }
}
factorial(int number)
{
    // the function is correct and it's been examined before....
}