重载运算符>&燃气轮机;() 我的C++知识很小,因为我只上了几节课。我了解了使用friend函数重载具有x,y实例变量的点对象的“simple book example”的输入、输出流操作符的基本情况。我现在正在看一个真实的项目,试图理解有人写了什么,我发现了错误: error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [2]' (or there is no acceptable conversion) 1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1000): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)' [found using argument-dependent lookup] 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ]

重载运算符>&燃气轮机;() 我的C++知识很小,因为我只上了几节课。我了解了使用friend函数重载具有x,y实例变量的点对象的“simple book example”的输入、输出流操作符的基本情况。我现在正在看一个真实的项目,试图理解有人写了什么,我发现了错误: error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [2]' (or there is no acceptable conversion) 1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1000): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)' [found using argument-dependent lookup] 1> with 1> [ 1> _Elem=char, 1> _Traits=std::char_traits<char> 1> ],c++,operator-overloading,C++,Operator Overloading,以我有限的知识,不确定错误是什么。它似乎在抱怨内联friend std::istream&operator>>函数中的类型不正确,因为基本istream模板中出现了一些错误(我不确定那里发生了什么)。如有任何建议,将不胜感激。谢谢 首先,因为您是C++新手:不要太信任编译器错误消息。在复杂的代码(模板、重载…)中,您经常会收到与实际问题无关的消息 结合对我的回答的评论:在你的例子中,“,”和类似的对象是常量,X,Y,Z的操作符只在非常量对象上声明,因此不适用。你要么复制它,要么只写一个常量版本。

以我有限的知识,不确定错误是什么。它似乎在抱怨内联friend std::istream&operator>>函数中的类型不正确,因为基本istream模板中出现了一些错误(我不确定那里发生了什么)。如有任何建议,将不胜感激。谢谢

首先,因为您是C++新手:不要太信任编译器错误消息。在复杂的代码(模板、重载…)中,您经常会收到与实际问题无关的消息


结合对我的回答的评论:在你的例子中,“,”和类似的对象是常量,X,Y,Z的操作符只在非常量对象上声明,因此不适用。你要么复制它,要么只写一个常量版本。

我很确定你不能像那样参数化istream提取。不过,如果被证明是错的,那就太酷了:)

试试这个:

void input(std::istream &in = std::cin)
{
char t;
in >> t >> X >> t >> Y >> t >> Z >> t;
}

不能以这种方式使用字符串作为输入。您将需要使用以下内容

char c;
cin.get() >> c;
if (c != '(') throw SomeError;
cin >> X;
cin.get() >> c;
if (c != ',') throw SomeError;
cin >> Y;

依此类推。

basic\u istream
istream
背后的真正类——定义类似于:

namespace std { 
typedef basic_istream<char> istream;
typedef basic_ostream<char> istream;
typedef basic_istream<wchar_t> wistream;
typedef basic_ostream<wchar_t> wostream;
}
您似乎想要读取一个字符,并让它与一个左括号匹配,并且(可能)失败,如果它没有找到。然而,不管是好是坏,
istream
s实际上并不直接支持这一点。要做到这一点,您可以这样做:

char ch;
some_istream >> ch;
if (ch != '(')
   // handle failure
或者,您可以只读取一个字符,并假设它是不需要检查就应该存在的。这还可以允许更灵活的输入格式,因此“1、2、3”和“1、2、3”都可以接受。对于某些情况(例如,由一个人直接输入的输入),这可能非常有用。在其他情况下(例如,来自另一个程序的输入),输入中的任何偏差都表明存在问题。

问题在于:

in >> "("
从概念上讲,这毫无意义。您正在尝试将输入读入字符串文字。(这就像试图将输入读入常量
5
)错误是因为字符串文字是一个常量字符数组(因此是
const char[2]
类型),因此无法读入

您可能需要:

void input(std::istream &in = std::cin)
{
    char dummy = 0;
    in >> dummy >> X >> dummy >> Y >> dummy >> Z >> dummy;
}
所有这些操作都是将这些字符读入一个伪变量。这是可行的,但这些角色可能是任何东西

相反,您可能应该执行以下操作:

// if you do it more than once, make it a function
bool expect_char(std::istream& in, char expected)
{
    char check = 0;
    in >> check;

    if (check != expected)
    {
        in.putback(); // replace it
        in.clear(std::ios::failbit); // set fail bit

        return false;
    }
    else
    {
        return true;
    }
}

void input(std::istream &in = std::cin)
{
    if (!in) // if stream is bad, just return
        return; 

    if (!expect_char(in, '('))
        return;

    in >> X;

    if (!expect_char(in, ','))
        return;

    in >> Y;

    if (!expect_char(in, ','))
        return;

    in >> Z;

    if (!expect_char(in, ')'))
        return;
}
现在唯一的问题是,如果提取中途失败,我们已经修改了点对象。理想情况下,要么全有,要么什么都没有。我们存储中间值,然后在一切正常时复制它们:

void input(std::istream &in = std::cin)
{
    if (!in)
        return; 

    if (!expect_char(in, '('))
        return;

    int newX; // or whatever type it is
    in >> newX;

    if (!expect_char(in, ','))
        return;

    int newY;
    in >> newY;

    if (!expect_char(in, ','))
        return;

    int newZ;
    in >> newZ;

    if (!expect_char(in, ')'))
        return;

    X = newX;
    Y = newY;
    Z = newZ;
}

我们得到了我们想要的。

字符串文字是常量,所以是的,“(“,”)和“,”是常量。在这种情况下,它与真正的问题直接相关:no
operator>
在左边取一个流,
常量字符[2]
右侧。您通常不希望流操作抛出。永远不要理解使用朋友的基本情况…不管这意味着什么<代码>:)Mhmm。我肯定前几天我在这里贴了一个班级,读了一个来自istream的特殊角色,但现在我找不到了。(搜索真的很糟糕。除了谷歌,没有全文搜索功能,而且不允许数据范围。)
void input(std::istream &in = std::cin)
{
    char dummy = 0;
    in >> dummy >> X >> dummy >> Y >> dummy >> Z >> dummy;
}
// if you do it more than once, make it a function
bool expect_char(std::istream& in, char expected)
{
    char check = 0;
    in >> check;

    if (check != expected)
    {
        in.putback(); // replace it
        in.clear(std::ios::failbit); // set fail bit

        return false;
    }
    else
    {
        return true;
    }
}

void input(std::istream &in = std::cin)
{
    if (!in) // if stream is bad, just return
        return; 

    if (!expect_char(in, '('))
        return;

    in >> X;

    if (!expect_char(in, ','))
        return;

    in >> Y;

    if (!expect_char(in, ','))
        return;

    in >> Z;

    if (!expect_char(in, ')'))
        return;
}
void input(std::istream &in = std::cin)
{
    if (!in)
        return; 

    if (!expect_char(in, '('))
        return;

    int newX; // or whatever type it is
    in >> newX;

    if (!expect_char(in, ','))
        return;

    int newY;
    in >> newY;

    if (!expect_char(in, ','))
        return;

    int newZ;
    in >> newZ;

    if (!expect_char(in, ')'))
        return;

    X = newX;
    Y = newY;
    Z = newZ;
}