problem with friends

This is a discussion on problem with friends within the C++ Programming forums, part of the General Programming Boards category; hey there, im having some trouble with the "friends". When i compile this piece of code on msvc++ it gives ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    problem with friends

    hey there, im having some trouble with the "friends". When i compile this piece of code on msvc++ it gives me errors, when it really shouldnt. Is there something that has to be done to correct it?

    Code:
    #ifndef H_videoType
    #define H_videoType
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class videoType 
    {     
    	friend ostream& operator<<(ostream&, const videoType&);
    
    public:
        void setVideoInfo(string title, string star1, 
                          string star2, string producer, 
                          string director, string productionCo, 
                          int setInStock);
    	    //Function to set the details of a video. 
    		//Private data members are set according to the parameters.
    		//Postcondition: videoTitle = title; movieStar1 = star1;  
    		//               movieStar2 = star2; movieProducer = 
    		//               producer; movieDirector = director; 
    		//               movieProductionCo = productionCo; 
    		//               copiesInStock = setInStock
    
        int getNoOfCopiesInStock() const;
    	    //Function to check the number of copies in stock. 
    	    //Postcondition: The value of the data member 
     	    //               copiesInStock is returned.     
    
    	void checkOut();
    	    //Function to rent a video. 
    	    //Postcondition: The number of copies in stock is 
            //               decremented by one.
    
        void checkIn();
    	    //Function to check in a video. 
    	    //Postcondition: The number of copies in stock is 
     	    //               incremented by one.     
    
    	void printTitle() const;
    	    //Function to print the title of a movie.   
    
    	void printInfo() const;
    	    //Function to print the details of a video.
    	    //The title of the movie, stars, director, and so on are
    	    //displayed on the screen.
    
         bool checkTitle(string title);
    	     //Function to check whether the title is the same as the
    		//title of the video.
    	    //Postcondition: Returns true if the title is the same as 
     	    //               the title of the video; 
    		//                otherwise, returns false.   
    
    	 void updateInStock(int num);
    	    //Function to increment the number of copies in stock by 
    	    //adding the value of the parameter num.
    	    //Postcondition: copiesInStock = copiesInStock  + num     
    
    	 void setCopiesInStock(int num);
    	    //Function to set the number of copies in stock.
    	    //Postcondition: copiesInStock = num
    
        string getTitle() const;
    	    //Function to return the title of the video.
     	    //Postcondition: The title of the video is returned.
    
         videoType(string title = "", string star1 = "", 
                  string star2 = "", string producer = "", 
                  string director = "", string productionCo = "", 
          	      int setInStock = 0);
    	     //constructor
    	     //Private data members are set according to the 
     	     //incoming parameters. If no values are specified, the 
     	     //default values are assigned.
    		//Postcondition: videoTitle = title; movieStar1 = star1; 
    		//               movieStar2 = star2; movieProducer = producer;
    		//               movieDirector = director;
    		//               movieProductionCo = productionCo;
    		//               copiesInStock = setInStock
    
    	    //Overloads the relational operators.
        bool operator==(const videoType&) const;
        bool operator!=(const videoType&) const;
        bool operator<(const videoType&) const;
            //operator< must be defined for videos in order for 
            //the sort function to work
    
    private:
        string videoTitle;		//variable to store the name 
      							//of the movie
        string movieStar1;		//variable to store the name 
     							//of the star
        string movieStar2;		//variable to store the name 
     							//of the star 
        string movieProducer;   //variable to store the name 
      							//of the producer 
        string movieDirector;   //variable to store the name 
                                //of the director 
        string movieProductionCo;  //variable to store the name 
                                   //of the production company 
        int copiesInStock; 		//variable to store the number of 
     							//copies in stock
    };
    
    #endif
    .cpp file
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    #include "videoType.h"
    
    using namespace std;
    
    void videoType::setVideoInfo(string title, string star1,  				    
    							 string star2, string producer,  				    
    							 string director, string productionCo, 				    
    							 int setInStock) 
    { 	     
    	videoTitle = title; 	     
    	movieStar1 = star1;
    	movieStar2 = star2;
    	movieProducer = producer;
    	movieDirector = director;
    	movieProductionCo = productionCo;
    	copiesInStock = setInStock;
    }
    
    void videoType::checkOut()
    {
    	if(copiesInStock > 0)
    	   copiesInStock--;
    	else
    	   cout<<"Currently out of stock"<<endl;
    }
    
    void videoType::checkIn()
    {
    	copiesInStock++;
    }
    
    int videoType::getNoOfCopiesInStock() const
    {
    	return copiesInStock;
    }
    
    void videoType::printTitle() const
    {
        cout<<"Video Title: "<<videoTitle<<endl;
    }
    
    void videoType::printInfo() const
    {
    	cout<<"Video Title: "<<videoTitle<<endl;
    	cout<<"Stars: "<<movieStar1<<" and "<<movieStar2<<endl;
    	cout<<"Producer: "<<movieProducer<<endl;
    	cout<<"Director: "<<movieDirector<<endl;
    	cout<<"Production Company: "<<movieProductionCo<<endl;
    	cout<<"Copies in stock: "<<copiesInStock<<endl;
    }
    
    bool videoType::checkTitle(string title)
    {
    	return(videoTitle == title);
    }
    
    void videoType::updateInStock(int num)
    {
    	copiesInStock += num;
    }
    
    void videoType::setCopiesInStock(int num)
    {
    	 copiesInStock = num;
    }
    
    string videoType::getTitle() const
    {
    	 return videoTitle;
    }
    
    videoType::videoType(string title, string star1, 
         	             string star2, string producer, 
           	             string director,
    	                 string productionCo, int setInStock)
    {
    	setVideoInfo(title, star1, star2, producer, director,
    			     productionCo, setInStock);
    }
    
    bool videoType::operator==(const videoType& other) const
    {
        return (videoTitle == other.videoTitle);
    }
    
    bool videoType::operator!=(const videoType& other) const
    {
    	return (videoTitle != other.videoTitle);
    }
    
    bool videoType::operator<(const videoType& other) const
    {
    	return (videoTitle < other.videoTitle);
    }
    
    ostream& operator<<(ostream& os, const videoType& video)
    {
    	os<<endl;
    	os<<"Video Title: "<<video.videoTitle<<endl;
    	os<<"Stars: "<<video.movieStar1<<" and "
    	  <<video.movieStar2<<endl;
    	os<<"Producer: "<<video.movieProducer<<endl;
    	os<<"Director: "<<video.movieDirector<<endl;
    	os<<"Production Company: "<<video.movieProductionCo<<endl;
    	os<<"Copies in stock: "<<video.copiesInStock<<endl;
    	os<<"_____________________________________"<<endl;
    	return os;
    }

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    hey there, im having some trouble with the "friends"
    Try hanging out with a different group of kids. </spam>
    Away.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    161
    There is no prototype for the function in your header file.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    what do u mean by no prototype??

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    bump, what do u mean by no prototype?

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Don't bump your own threads.

    You have to declare the function before the first time you call it, otherwise the compiler won't know that the function exists.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    sorry abt that, but i have declared the function in the header file, msvc++ complains, gives errors, while linux doesnt......why???

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Put the declaration in header file under "public"

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    since it is friends, it shouldnt be public, the whole reason for friends is that it can access private members.

    well i did my own research and found that there is bug in microsoft visual c++. The last service pack for this can be downloaded from their website which cures the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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