Thread: help with overloading operators

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    help with overloading operators

    ok i am doing something for school and part of it requires overloading the << operator. however i cannot for the life of me get this thing to work. the top code is the example he gave us and the bottom is what i am writing. they look the exact same other than the names and variables.

    the error i get when i run the program is the

    Error 15 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'MyVector' (or there is no acceptable conversion) c:\users\tachikoma\documents\visual studio 2010\projects\project13\project13\driver.cpp 39
    Code:
    	friend ostream& operator<<( ostream& theStream, const MyString& aString)
    	{
    		for (int i = 0; i < aString.size; i++)
    		{
    			theStream << aString.theString[i];
    		}
    		return theStream;
    	}
    Code:
    std::ostream& operator<< (std::ostream& out,const MyVector& myVec)
    	{
    		for (int i = 0; i < myVec.vectorSize; i++)
    		{
    			out << myVec.vectorPointer[i] << std::endl ;
    		}
    		return out;
    	}
    ive tried sticking this in the implentation cpp file and the .h cpp file. i cannot edit the cpp file with the main() because thats provided and is used to check if this works or not. also his example is inside of one file class and all and the example is declared inside of the class using friend. seems i cant find where to stick it and get it to work? i tried using friend also but that didnt work didnt think it would but im probably doing it wrong

    also do i need a prototype somewhere? if so where?

    any help would be greatly appreciated
    hooch

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Since the top example works, then do it as similar to that as possible. I.e. declare it as friend and put it inside the header.
    Show the line of code that the error refers to, and the declarations of any variables involved in that line.
    There may be other stuff that we need to see also.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    it errors 5 times with that same error.

    here is the driver.cpp file

    Code:
    #include <iostream>
    #include "MyVector.h"     
    using namespace std;
    
    // the printV function
    // used to test the copy constructor
    // parameter: a MyVector object
    void printV(MyVector);
    
    int main( )
    {
    	cout << "\nCreating a vector Sam of size 4.";
    	MyVector sam( 4 );
    
    	cout << "\nPush 12 values into the vector.";
    	for (int i = 0; i < 12; i++)
    		sam.push_back(i);
    
    	cout << "\nHere is sam: ";
    	cout << sam;   //ERRORS HERE
    	cout << "\n---------------\n";
    
    		cout << "\nCreating a vector Joe of size 4.";
    	MyVector joe( 4 );
    	cout << "\nPush 6 values into the vector.";
    	for (int i = 0; i < 6; i++)
    		joe.push_back(i * 3);
    
    	cout << "\nHere is joe: ";
    	cout << joe;//ERRORS HERE
    	cout << "\n---------------\n";
    	
    	cout << "\nTest the overloaded assignment operator \"joe = sam\": ";
    	joe = sam;
    
    	cout << "\nHere is sam: ";
    	cout << sam;//ERRORS HERE
    	cout << "\n---------------\n";
    
    	cout << "\nHere is joe: ";
    	cout << joe;//ERRORS HERE
    	cout << "\n---------------\n";
    
    	// pass a copy of sam by value
    	printV(sam);
    	
    
    	cout << endl;
    	system("PAUSE");
    	return 0;
    }
    
    void printV(MyVector v)
    {
    	cout << "\n--------------------\n";
    	cout << "Printing a copy of a vector\n";
    	cout << v;//ERRORS HERE
    }
    i added a comment on each line it errors. also dont forget i cannot change this piece of code here at all.
    hooch

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You need to have a declaration of the operator<<() in MyVector.h (eg a friend declaration in the MyVector class). Putting it only in a source file (and not in a header) is often not enough.

    Implementation of the operator<< can be placed in one (and only one) of the source files in your project, after that source file performs #include "MyVector.h"

    Don't rely on a "using namespace" directive being in effect (unless you like the potential of ambiguous code that the compiler will unilaterally reject). That means declare and implement the operator with a first argument of type std::ostream &, not just an ostream &.
    Last edited by grumpy; 04-07-2011 at 02:28 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    oh i just stuff namepace up there trying to see if that would help.


    sorry im still lost where would i stick the declaration what exactly would that look like? would declaration look like this?

    Code:
    friend std::ostream& std::operator<<(std::ostream&,const MyVector);
    just sticking it in the .h file doesnt seem to do anything but produce more errors? sticking it in the .cpp produces more errors?
    hooch

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ssjnamek View Post
    oh i just stuff namepace up there trying to see if that would help.
    Trying different coding tricks at random without actually understanding what they do tends to cause more problems than it solves. Such practices are the hallmark of undisciplined amateurs.

    Employing "using namespace std;" in a header file may not cause problems immediately but, in the long run, causes a lot of problems.

    Quote Originally Posted by ssjnamek View Post
    sorry im still lost where would i stick the declaration what exactly would that look like? would declaration look like this?

    Code:
    friend std::ostream& std::operator<<(std::ostream&,const MyVector);
    just sticking it in the .h file doesnt seem to do anything but produce more errors? sticking it in the .cpp produces more errors?
    That's right. Again you are demonstrating that random and undisciplined hacking of code tends to create more problems than it solves.

    A friend declaration is only valid within a class declaration.
    Code:
    //  friend declarations are invalid here
    
    class MyVector
    {
          //   friend declarations go here  (they declare friends of class MyVector)
    };
    
    //   friend declarations are invalid here.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    well its not entirely my fault with 3 weeks left in this class pointers references and creating my own vector get shoved down our throats. when those are easily by far the most complicated part of programming to this point heck ive heard pointers are the most complicated part of programming in general. and ive never known anyone to understand all of that in a matter of weeks with relative ease. so yes my apologies my noobness but i do appreciate the help!

    alright well i stuck it in the class declaration as you said.

    Code:
    	friend std::ostream& operator<< (std::ostream& out,const MyVector& myVec);
    	{
    		for (int i = 0; i < myVec.vectorSize; i++)
    		{
    			out << myVec.vectorPointer[i] << std::endl ;
    		}
    		return out;
    	}
    then stuck this in the cpp implement file
    Code:
    	std::ostream& operator<< (std::ostream& out,const MyVector& myVec)
    	{
    		for (int i = 0; i < myVec.vectorSize; i++)
    		{
    			out << myVec.vectorPointer[i] << std::endl ;
    		}
    		return out;
    	}


    also noticed my teachers example sticks it in the class as well(though he is only using one file in his example but still shouldnt make a difference). well yes i did the random code hacking and tried it it still errors but i would think since this is an implenment file it should should with the other implements? am i wrong?

    11 IntelliSense: member "MyVector::vectorSize" (declared at line 25 of "c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\MyVector.h") is inaccessible c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.cpp 177
    12 IntelliSense: member "MyVector::vectorPointer" (declared at line 24 of "c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\MyVector.h") is inaccessible c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.cpp 179
    Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.h 48
    Error 7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.h 48
    Error 8 error C2805: binary 'operator <<' has too few parameters c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.h 48
    Error 3 error C2433: 'ostream' : 'friend' not permitted on data declarations c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.h 48
    Error 9 error C2248: 'MyVector::vectorSize' : cannot access private member declared in class 'MyVector' c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.cpp 177
    Error 10 error C2248: 'MyVector::vectorPointer' : cannot access private member declared in class 'MyVector' c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.cpp 179
    Error 2 error C2143: syntax error : missing ';' before '&' c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.h 48
    Error 6 error C2061: syntax error : identifier 'ostream' c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.h 48
    Error 1 error C2039: 'ostream' : is not a member of 'std' c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.h 48
    Error 5 error C2039: 'ostream' : is not a member of 'std' c:\users\tachikoma\documents\visual studio

    2010\projects\project13\project13\myvector.h 48


    of course this leads to other errors. should the function body be in the implement cpp file? does it matter?

    i dont even understand some of these errors. it says ostream isnt part of std but it clearly is cause it whines if you take std off and std:: even has ostream in the auto fill in box what really confuses me is this is error 1 if that matters? same with operator it whines if i put it there yet its in the fill in box.

    yes i know 1 error can create like 100 other crazy ones. but still these seem to make little sense? like friend not permitted in data declarations? also if i put the entire function in the MyVector.h it doesnt care if my variables are private but in the .cpp it does?

    yes i know im rather lost i appreciate your help though! i really think had this not all been crammed down in the last 3 weeks of class id be far less confused. oh well no use whining about it now
    hooch

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ssjnamek View Post
    well its not entirely my fault with 3 weeks left in this class pointers references and creating my own vector get shoved down our throats.
    If you have failed to learn while the class is underway, one person who deserves to be blamed is the one you look at in the mirror.

    As to your problem with code, remove the body (i.e. implementation) of the operator function from the header file.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The trick with solving compile errors is to solve them in the order they are generated. When doing it this way you most often fix the first few and the rest are no longer errors.
    A compiler can produce a hundred errors for something as simple as a missing semi-colon, and guess which one of those errors will actually be the one that shows the closest to where the real problem is?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    oh i certainly do take some of the blame. but admittely this class is poorly designed the teacher who has no control over that(department has the say so) has admitted as much. but yes i do blame myself for at least some of my failure to understand it.

    that said it seems the source of my problems was not including the <fstream> in the header file. never would of known that especially considering the example in class didnt even remotely drop any hint at that.

    but thanks for the help you at least got me to stick the stuff in the right place appreciate it!
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 08:24 PM