0

So I have this code written by myself but taken from other example codes...

class A{
    friend std::ostream& operator<< (std::ostream& out, A& a);
    // Constructors, destructor, and variables have been declared
    // and initialized and all good.
}

std::ostream& operator<< (std::ostream& out, A& a){
    out << " this gets written " << endl; // it doesn't get executed
    return out;
}

int main(){
    A *_a = new A();
    return 0;
}

And well, this is just not printing in the console " this gets written "

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Ren
  • 4,594
  • 9
  • 33
  • 61
  • 3
    Your code makes no sense. Your `<<` operator accepts an argument of type `AccountInfo&`, not `A*`, and you're not even showing us the part where you actually try to *use* the operator. You've omitted so much code that was is left is useless for debugging your problem. – user229044 Feb 02 '12 at 20:18
  • _use_? I thought it was kind of automatic, like when an object gets created. Also the AccountInfo type is a mistake, I will fix it now. – Ren Feb 02 '12 at 20:20
  • 1
    By "use" I mean, "show us how you're attempting to write your object to a stream". You can't just declare the operator and expect it to do something, you have to actually invoke it. What are you *expecting* the above code to do? The only code you've posted, `A *_a = new A();`, has nothing to do with a `<<` operator or streams. It just makes a new `A` and then exits. – user229044 Feb 02 '12 at 20:22
  • umm well I have the `friend std::ostream& operator<< (std::ostream& out, A& a);` inside the class, I thought that would write it into the console every time a new object was created or something like that. – Ren Feb 02 '12 at 20:28
  • No, that's not at all what that does. Google it, `friend` is for something completely different. – user229044 Feb 02 '12 at 20:30
  • @Yokhen: If you want code do be executed when an object is created, you'll want to put it in a **constructor**, not `operator<<`. The constructor is executed at object creation time. – Drew Dormann Feb 02 '12 at 20:49
  • 3
    I think it's time for a [good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)! – Kerrek SB Feb 02 '12 at 20:54

1 Answers1

1

If you're attempting to use the operator via std::cout << a or something similar, the problem is you're passing a pointer to an object, while the << operator is defined as taking a reference to an object. You either need to declare a as regular (non-pointer) A, or use std::cout << *a.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • where should I use `std::cout << *a` so that it's written every time that a new A object is created it's printed? – Ren Feb 02 '12 at 20:26
  • 1
    Inside the class's constructor. You would write something like `std::cout << *this`. – user229044 Feb 02 '12 at 20:30
  • Also @meagar, whenever I set `a` as a regular `A` and use `std::cout << *a` I always get the address anyway. The only case when the address isn't printed is when I don't change the type of `a` and use `std::cout << *a` – Ren Feb 02 '12 at 20:48
  • @Yokhen : If you instantiate a regular `A` vs. an `A*`, `std::cout << *a` wouldn't compile; the correct syntax would be `std::cout << a`. – ildjarn Feb 02 '12 at 20:51