C ++ ostream运算符<<超载

时间:2017-11-09 15:40:08

标签: c++ overloading operator-keyword

我有一张带参数的卡片(颜色,值)和输出卡片参数的方法。

错误二进制'<<' :找不到哪个操作符采用了类型' CardColor'的右手操作数。和' CardValue'(或没有可接受的转换)

Card.h

struct Card{
    Color CardColor;
    Value CardValue;

    Card(Color color, Value cardValue) {
        this->CardColor = color;
        this->CardValue = cardValue;
    }
    public:
    friend std::ostream &operator<<(std::ostream &output, const Card c);
};

Value.h

#ifndef VALUE_H
#define VALUE_H
enum class Value{ SEDMA, OSMA, DEVITKA, DESITKA, SPODEK, KRAL, ESO, SVRSEK };
#endif

Color.h

#ifndef COLOR_H
#define COLOR_H
enum class Color { ZALUD, LISTY, SRDCE, KULE };
#endif

Main.cpp的

#include <Card.h>
std::ostream &operator<<(std::ostream &output, const Card c)
{
    output << c.CardColor << c.CardValue << std::endl;
    return output;
}

void outputCard(Card c)
{
    std::cout << c << std::endl;
}

1 个答案:

答案 0 :(得分:2)

发表评论:

  

二进制&#39;&lt;&lt;&# :找不到哪个操作符采用了类型&#39; CardColor&#39;的右手操作数。和&#39; CardValue&#39; (或者没有可接受的转换)

您还需要为“颜色和值”类提供 struct Data *data; pthread_t t1; while (contP!=numero2){ contP++; sprintf(pro, "t%d.txt", contP); strcpy(data->nomT,pro); printf("Aca esa el archivo salida:%s\n",pro); if (numero2-contP!=1){ lineasT=t/numero2; data->lineasT=lineasT; printf("(%d/%d) = %d \n",t,numero2,lineasT); } else{ //Si es el penultimo lineasT=(t/numero2)+(t%numero2); data->lineasT=lineasT; printf("(%d/%d)+(%dmod%d) = %d \n",t,numero2,t,numero2,lineasT); } int l = pthread_create(&t1,NULL,recorreNumeroH,(void *) data); printf("se creo? %d\n",l); } pthread_mutex_destroy(&lock);

Color类的示例:

operator<<