Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模糊运算符<&书信电报; #包括“stdafx.h” #包括“Record.h” 模板//如果我使用常规fnc而不是模板,则编译 //否则我会得到一个错误(列在最下面)说 //该运算符_C++_Operator Overloading - Fatal编程技术网

C++ 模糊运算符<&书信电报; #包括“stdafx.h” #包括“Record.h” 模板//如果我使用常规fnc而不是模板,则编译 //否则我会得到一个错误(列在最下面)说 //该运算符

C++ 模糊运算符<&书信电报; #包括“stdafx.h” #包括“Record.h” 模板//如果我使用常规fnc而不是模板,则编译 //否则我会得到一个错误(列在最下面)说 //该运算符,c++,operator-overloading,C++,Operator Overloading,运算符运算符后面的概念运算符首先,您需要更仔细地阅读错误消息。作为另一种选择,考虑打破声明,这样的事情: #include "stdafx.h" #include "Record.h" template<class T>//If I make instead of template regular fnc this compiles //otherwise I'm getting an error (listed on the very bottom) saying //

运算符
运算符后面的概念
运算符首先,您需要更仔细地阅读错误消息。作为另一种选择,考虑打破声明,这样的事情:

#include "stdafx.h"
#include "Record.h"

template<class T>//If I make instead of template regular fnc this compiles  
//otherwise I'm getting an error (listed on the very bottom) saying  
// that operator << is ambiguous, WHY?
ostream& operator<<(ostream& out, const T& obj)
{
    out << "Price: " 
        << (obj.getPrice()) << '\t'//this line causes this error
        << "Count: "
        << obj.getCount()
        << '\n';
    return out;
}

int _tmain(int argc, _TCHAR* argv[])
{
    vector<Record> v;
    v.reserve(10);
    for (int i = 0; i < 10; ++i)
    {
        v.push_back(Record(rand()%(10 - 0)));
    }
    copy(v.begin(),v.end(),ostream_iterator<Record>(cout, "\n"));
    return 0;
}

//Record class
class Record
{
    private:
        int myPrice_;
        int myCount_;
        static int TOTAL_;
    public:
        Record(){}
        Record(int price):myPrice_(price),myCount_(++TOTAL_)
        {/*Empty*/}
        int getPrice()const
        {
            return myPrice_;
        }

        int getCount()const
        {
            return myCount_;
        }
        bool operator<(const Record& right)
        {
            return (myPrice_ < right.myPrice_) && (myCount_ < right.myCount_);
        }
};

int Record::TOTAL_ = 0;

out首先,您需要更仔细地阅读错误消息。作为另一种选择,考虑打破声明,这样的事情:

#include "stdafx.h"
#include "Record.h"

template<class T>//If I make instead of template regular fnc this compiles  
//otherwise I'm getting an error (listed on the very bottom) saying  
// that operator << is ambiguous, WHY?
ostream& operator<<(ostream& out, const T& obj)
{
    out << "Price: " 
        << (obj.getPrice()) << '\t'//this line causes this error
        << "Count: "
        << obj.getCount()
        << '\n';
    return out;
}

int _tmain(int argc, _TCHAR* argv[])
{
    vector<Record> v;
    v.reserve(10);
    for (int i = 0; i < 10; ++i)
    {
        v.push_back(Record(rand()%(10 - 0)));
    }
    copy(v.begin(),v.end(),ostream_iterator<Record>(cout, "\n"));
    return 0;
}

//Record class
class Record
{
    private:
        int myPrice_;
        int myCount_;
        static int TOTAL_;
    public:
        Record(){}
        Record(int price):myPrice_(price),myCount_(++TOTAL_)
        {/*Empty*/}
        int getPrice()const
        {
            return myPrice_;
        }

        int getCount()const
        {
            return myCount_;
        }
        bool operator<(const Record& right)
        {
            return (myPrice_ < right.myPrice_) && (myCount_ < right.myCount_);
        }
};

int Record::TOTAL_ = 0;

out错误的原因是您的模板重载与另一个模板重载冲突,没有理由选择一个模板而不是另一个模板:

out << "Price: ";
out << (obj.getPrice());
out << "\tCount: ";
out << obj.getCount();
out << '\n';
模板

basic_ostream&operator错误的原因是您的模板重载与另一个模板重载冲突,没有理由选择一个模板而不是另一个模板:

out << "Price: ";
out << (obj.getPrice());
out << "\tCount: ";
out << obj.getCount();
out << '\n';
模板

基本的\u ostream&operator这可能是因为没有为每种类型定义
getPrice()
,因此模板不工作。您能发布准确的错误消息吗?他们通常会告诉你确切的原因(尽管是以最全面的方式,使用模板)@Cogwheel我添加了我得到的全部错误消息。哈,这确实很有用,不是吗-_-这可能是因为没有为每种类型定义
getPrice()
,因此模板不工作。您可以发布准确的错误消息吗?他们通常会告诉你确切的原因(尽管是以最全面的方式,使用模板)@Cogwheel我添加了我得到的全部错误消息。哈,这确实很有用,不是吗-_-总的来说,我同意你的说法,但我不能同意你的说法,请稍等,让我复制一下。。。然后粘贴。。。o现在我们开始“编译器如何决定是否使用operatorWell,Jerry的回答(您接受了)没有说任何不同:“问题的出现是因为编译器不知道是使用普通重载打印字符串,还是使用定义的模板打印字符串。”这就是“含糊不清”的地方“意思是:根据语言的规则,编译器最终会有不止一个
运算符。一般来说,我同意你的看法。你说的话我不能同意,等一下,让我复制一下。。。然后粘贴。。。o现在我们开始“编译器如何决定是否使用operatorWell,Jerry的回答(您接受了)没有说任何不同:“问题的出现是因为编译器不知道是使用普通重载打印字符串,还是使用定义的模板打印字符串。”这就是“含糊不清”的地方“意思是:使用语言规则,编译器最终会有多个
运算符,请您解释。所以我猜编译器提供的行号是错误的?但什么让我困惑,为什么编译器没有为string和int选择专门的版本?@A-ha:对于编译器来说,在实际问题的位置之后列出一两行是非常典型的(通常是当它清楚地表明已经处理的内容有问题时). 它不能选择一个而不是另一个,因为两者都不需要转换,所以它们是同样好的匹配项。谢谢你的解释。所以我猜编译器提供的行号是错误的?但什么让我困惑,为什么编译器没有为string和int选择专门的版本?@A-ha:对于编译器来说,在实际问题的位置之后列出一两行是非常典型的(通常是当它清楚地表明已经处理的内容有问题时). 它不能选择一个而不是另一个,因为两者都不需要转换,所以它们是同样好的匹配项。