why are my operators not working

closed account (SGTCko23)
operator >>Postconditions are
// -The red value of pix is set to the first integer.
// -The green value of pix is set to the second integer.
// -The blue value of pix is set to the third integer.
operator<< Stream output operator (<<) for outputting the contents of a pixel.
// Example: A pixel with red value 100, green value 150, and blue value 200
// will produce the following output:100 150 200
// Note: Returns the `out` parameter to allow chaining of stream output.

Pixel.h

#ifndef PIXEL_H
#define PIXEL_H

namespace imagelab
{
class Pixel
{
public:
Pixel( int r=0, int g=0, int b=0 );
void set( int r, int g, int b );
void setRed( int r );
void setGreen( int g );
void setBlue( int b );
int getRed( ) const;
int getGreen( ) const;
int getBlue( ) const;

private:
int rval; // red 0-255
int gval; // green 0-255
int bval; // blue 0-255
};

bool operator== (const Pixel& pix1, const Pixel& pix2);
bool operator!= (const Pixel& pix1, const Pixel& pix2);
std::ostream& operator<< (std::ostream& out, const Pixel& pix);
std::istream& operator>> (std::istream& in, Pixel& pix);
}

Pixel.cpp

#include <iostream>
#include <sstream>
#include "Pixel.h"
#include <cassert>

using namespace std;

namespace imagelab
{
Pixel::Pixel( int r, int g, int b )
{
assert(0<= r && r<= 255);
setRed(rval);
assert(0<= g && g<= 255);
setGreen(gval);
assert(0<= b && b<= 255);
setBlue(bval);
//assert(0 <= r <= 255; 0 <= g <= 255; 0 <= b <= 255)
}

void Pixel::set( int r, int g, int b )
{
rval = r;
gval = g;
bval = b;
}

void Pixel::setRed( int r )
{
rval = r;
}
void Pixel::setGreen( int g )
{
gval = g;
}
void Pixel::setBlue( int b )
{
bval = b;
}
int Pixel::getRed( ) const
{
return(rval);
}
int Pixel::getGreen( ) const
{
return(gval);
}
int Pixel::getBlue( ) const
{
return(bval);
}
bool operator==(const Pixel& pix1, const Pixel& pix2)
{
if (pix1 == pix2)
{
return(false);
}
}
bool operator!=(const Pixel& pix1, const Pixel& pix2)
{
if(pix1 != pix2)
{
return(true);
}
}
std::ostream& operator<<(std::ostream& out, const Pixel& pix)
{
out >> pix.getRed() >> pix.getGreen() >> pix.getBlue();
return(out);
}
std::istream& operator>>(std::istream& out, Pixel& pix)
{
out >> pix.setRed(0) >> pix.setGreen(0) >> pix.setBlue(0);
return(out);
}


}


error
Pixel.cpp: In function ‘std::ostream& imagelab::operator<<(std::ostream&, const imagelab::Pixel&)’:
Pixel.cpp:79:11: error: no match for ‘operator>>’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘int’)
out >> pix.getRed() >> pix.getGreen() >> pix.getBlue();
^
Pixel.cpp:79:11: note: candidates are:
In file included from Pixel.cpp:14:0:
Pixel.h:113:19: note: std::istream& imagelab::operator>>(std::istream&, imagelab::Pixel&)
std::istream& operator>> (std::istream& in, Pixel& pix);
^
Pixel.h:113:19: note: no known conversion for argument 1 from ‘std::ostream {aka std::basic_ostream<char>}’ to ‘std::istream& {aka std::basic_istream<char>&}’
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/iostream:40:0,
from Pixel.cpp:12:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/istream:779:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/istream:779:5: note: template argument deduction/substitution failed:
Pixel.cpp:79:25: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
out >> pix.getRed() >> pix.getGreen() >> pix.getBlue();
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/iostream:40:0,
from Pixel.cpp:12:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/istream:774:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/istream:774:5: note: template argument deduction/substitution failed:
Pixel.cpp:79:25: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
out >> pix.getRed() >> pix.getGreen() >> pix.getBlue();
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/iostream:40:0,
from Pixel.cpp:12:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/istream:732:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/istream:732:5: note: template argument deduction/substitution failed:
Pixel.cpp:79:25: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
out >> pix.getRed() >> pix.getGreen() >> pix.getBlue();
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/iostream:40:0,
from Pixel.cpp:12:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/istream:727:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/istream:727:5: note: template argument deduction/substitution failed:
Pixel.cpp:79:25: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<char, _Traits>’
out >> pix.getRed() >> pix.getGreen() >> pix.getBlue();
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/istream:882:0,
from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/iostream:40,
from Pixel.cpp:12:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/bits/istream.tcc:923:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/bits/istream.tcc:923:5: note: template argument deduction/substitution failed:
Pixel.cpp:79:25: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
out >> pix.getRed() >> pix.getGreen() >> pix.getBlue();
^
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/istream:882:0,
from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/iostream:40,
from Pixel.cpp:12:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/bits/istream.tcc:955:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/bits/istream.tcc:955:5: note: template argument deduction/substitution failed:
Pixel.cpp:79:25: note: ‘std::ostream {aka std::basic_ostream<char>}’ is not derived from ‘std::basic_istream<_CharT, _Traits>’
out >> pix.getRed() >> pix.getGreen() >> pix.getBlue();
^



Last edited on
You are using >> when you should be using <<.
closed account (SGTCko23)
now I am getting the error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Pixel.cpp:84:25: error: no matching function for call to ‘imagelab::Pixel::setRed()’
       out >> pix.setRed() >> pix.setGreen() >> pix.setBlue();
                         ^
Pixel.cpp:84:25: note: candidate is:
Pixel.cpp:39:10: note: void imagelab::Pixel::setRed(int)
     void Pixel::setRed( int r )
          ^
Pixel.cpp:39:10: note:   candidate expects 1 argument, 0 provided
Pixel.cpp:84:43: error: no matching function for call to ‘imagelab::Pixel::setGreen()’
       out >> pix.setRed() >> pix.setGreen() >> pix.setBlue();
                                           ^
Pixel.cpp:84:43: note: candidate is:
Pixel.cpp:43:10: note: void imagelab::Pixel::setGreen(int)
     void Pixel::setGreen( int g )
          ^
Pixel.cpp:43:10: note:   candidate expects 1 argument, 0 provided
Pixel.cpp:84:60: error: no matching function for call to ‘imagelab::Pixel::setBlue()’
       out >> pix.setRed() >> pix.setGreen() >> pix.setBlue();
                                                            ^
Pixel.cpp:84:60: note: candidate is:
Pixel.cpp:47:10: note: void imagelab::Pixel::setBlue(int)
     void Pixel::setBlue( int b )
          ^
Pixel.cpp:47:10: note:   candidate expects 1 argument, 0 provided

You didn't follow @Peter87's suggestion at all.
1
2
3
4
std::ostream& operator<<(std::ostream& out, const Pixel& pix) {
    out << pix.getRed() << pix.getGreen() << pix.getBlue();
    return out;
}


EDIT:
Actually, I just noticed you're probably referring to your input stream... you should really name your parameters sensibly.

In this case, store the input and then set:
1
2
3
4
5
6
std::istream& operator>>(std::istream& in, Pixel& pix) {
    int r, g, b;
    in >> r >> g >> b;
    pix.set(r, g, b);
    return in;
}
Last edited on
Topic archived. No new replies allowed.