Thread: Getting error when passing object to a function

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    92

    Getting error when passing object to a function

    Code:
    #include<iostream>
    #include <string>
    using namespace std;
    
    
    class Movie{
        private:
            string title;
            int year;
            int name;
            string director;
        public:
            void setTitle(string);
            void setYear(int);
            void setDirector(string);
            string displayTitle();
            int displayYear();
            string displayDirector();
    };
    
    
    void Movie::setTitle(string title){
        Movie::title = title;
    }
    void Movie::setYear(int year){
        Movie::year = year;
    }
    void Movie::setDirector(string director){
        Movie::director = director;
    }
    string Movie::displayTitle(){
        return (Movie::title);
    }
    string Movie::displayDirector(){
        return (Movie::director);
    }
    int Movie::displayYear(){
        return (Movie::year);
    }
    
    
    
    
    int main(){
        
        Movie myFavoriteMovie;
        string direktor = "Markus Schultze";
        string film = "In Zukunft besser machen";
        int yearr = 1975;
        void display(Movie);
        
        myFavoriteMovie.setDirector(direktor);
        myFavoriteMovie.setTitle(film);
        myFavoriteMovie.setYear(yearr);
        
        void display(myFavoriteMovie);
        
        
        return 0;
    }
    
    
    void display(Movie favMovie){
        cout << favMovie.displayDirector() << endl << favMovie.displayTitle() << endl << favMovie.displayYear();
    }
    Compiler tells the message " Line 56 variable or field 'display' declared void"

    Not quite sure why I get this error.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You don't need the return type when you call a function. Also, while not syntactically incorrect, line 50 should probably be moved outside of main().

    Jim

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by jimblumberg View Post
    You don't need the return type when you call a function. Also, while not syntactically incorrect, line 50 should probably be moved outside of main().

    Jim
    I didn't even notice that I added return type when calling a function, this sleep deprivation is taking effect obviously , thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Array of Object gives compilation ERROR
    By vishalonne in forum C++ Programming
    Replies: 10
    Last Post: 12-24-2012, 09:09 AM
  2. Replies: 6
    Last Post: 04-04-2010, 11:48 AM
  3. Passing A Function From One Object to Another
    By HalNineThousand in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2008, 07:26 PM
  4. Passing object to function outside of loop
    By HLMetroid in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2008, 03:22 PM
  5. Passing ofstream object to another function
    By otika in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 09:28 PM

Tags for this Thread