English 中文(简体)
Overloading Insertion Operator in C++
原标题:

I have a class in which I m trying to overload the << operator. For some reason, it is not being overloaded.

Here is my .h file:

friend std::ostream& operator<<(std::ostream&, const course &); //course is my class object name

in my .cpp, I have this as my implementation:

std::ostream& operator<<(std::ostream &out, const course & rhs){
    out << rhs.info;
    return out;
}

This should be correct, but when I try to compile it, it says that cout << tmp; is not defined in ostream.

I ve included iostream in my .cpp and .h

I m been pulling my hair out trying to figure this out. Can you see anything that s wrong with this?

EDIT: Since what I m doing seems to be correct, here s all of my source code: http://pastebin.com/f5b523770

line 46 is my prototype

line 377 is the implementation

line 435 is where it fails when i attempt to compile it.

also, I just tried compiling it on another machine, and it gives this error instead:

course.cpp:246: error: non-member function  std::ostream& operator<<(std::ostream&, const course&)  cannot have cv-qualifier
make: *** [course.o] Error 1
问题回答

The syntax you ve listed is correct, but the overloaded operator prototype has to be declared in the course definition to work properly.

course.h

class course {
public:
  friend std::ostream& operator<<(std::ostream&, const course&);
private:
  int info;
}

course.cpp

std::ostream& operator<<(std::ostream &out, const course &rhs){
  out << rhs.info;
  return out;
}

It looks fine to me. Here s my version of it:

course.h

#include <iostream>

class course
{
public:
    friend std::ostream& operator<<(std::ostream&, const course &); //course is my class object name
    int info;
    course(){info = 10;}
};

course.cpp

#include <iostream>    
#include "course.h"

std::ostream& operator<<(std::ostream &out, const course & rhs)
{
    out << rhs.info;
    return out;
}

main_file.cpp

#include <iostream>
#include "course.h"

int main()
{
    course a;
    std::cout<<a;

    return 0;
}

You should include the rest of the code, I don t think we can see where the problem is.

The following trivial example works:

class course
{
public:
    course(int info) : info(info) { }
    int info;

    friend std::ostream& operator<<(std::ostream&, const course &);
};

std::ostream& operator<<(std::ostream &out, const course & rhs)
{
    out << rhs.info;
    return out;
}

int main(int, char*[])
{
    course tmp(3);
    std::cout << tmp;
    return 0;
}

I found this helpful!

My insertion and extraction operators are slightly different.

here they are defined:

friend ostream& operator<<(ostream &fout, datatype toPrint)

friend istream& operator>>(istream &fin, datatype &toReadTo)

the syntax needs to be exact.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签