Stream function compile problem

This is a discussion on Stream function compile problem within the C++ Programming forums, part of the General Programming Boards category; Hi all Can anyone help me? I'm having a compile problem when attempting to compile code including declaration and definition ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    43

    Stream function compile problem

    Hi all

    Can anyone help me? I'm having a compile problem when attempting to compile code including declaration and definition of the following:

    ostream& operator << (ostream& output, const Dog& theDog);
    friend ostream& operator << (ostream& output, const Dog& theDog);

    The compiler (Visual Express 2008) keeps telling me that it cannot find the return type (int assumed - C++ does not support default int etc). Obviously I'm intending for the return type to be ostream&.

    Can anyone work out what's going on here? I am including "using std:stream" at the start of the program.

    Cheers! :-)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Post the smallest and simplest program that demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    43
    Hi

    I have included the whole dog class. Note that this is the first part of three C++ files that make up my project...
    Code:
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::ostream;
    
    class Dog
    {
    public:
    	Dog ();
    	Dog (int);
    	~Dog ();
    
    	int getAge () const {return itsAge;}
    	void setAge (int age) {itsAge = age;}
    
    	friend ostream& operator << (ostream& output, const Animal& theAnimal); 
    
    private:
    	int itsAge;
    };
    
    Dog::Dog () : itsAge(0)
    {
    	cout << "Dog default constructor...";
    }
    
    Dog::Dog (int age) : itsAge (age)
    {
    	cout << "Dog constructor with parameter...";
    }
    Dog::~Dog()
    {
    	cout << "Destructed a dog :-(...";
    }
    ostream& operator << (ostream& output, const Dog & rhs) 
    {
    	output << rhs.getAge();
    	return output;
    }
    Cheers :-)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Well, one obvious problem is that you declared the overloaded operator<< as:
    Code:
    friend ostream& operator << (ostream& output, const Animal& theAnimal);
    but the name Animal has not been declared at that point. You probably want to declare it as a non-member non-friend function:
    Code:
    ostream& operator << (ostream& output, const Dog& theAnimal);
    Other than that, it looks okay, but:
    • You #include <iostream> because you use std::cout to print some text to help you see how the constructors and destructor work. This is fine, but you should #include <ostream> as well because when you do remove the printing of those text, you should then remove <iostream> from the list of files included.
    • Do not use using declarations (e.g., using std::cin) in a header file except within some restricted scope.
    • Declare the constructor that takes an int as explicit since it probably does not make sense to write: Dog dog = 10;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21