Unable to pass object into function of another object?

Hi all,

I am trying to pass this object into a function of another class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Moviemanager class:
void MovieManager::addNewMovie(std::string url)
{
MovieRecord mr;
mr.getURL(url); //getURL method will populate various variables of mr object
MovieDB db1;
db1.addMovie(mr); //mr object is guaranteed NOT empty!
//retrieveMoviesByName("hehe");
}

MovieDB class:
void  MovieDB::addMovie(MovieRecord mr) 
{

    std::cout << "hehhe" << mr.getTitle() << "hoho"; //THIS MR OBJECT IS EMPTY
}

class MovieRecord {
public:
    MovieRecord(); //constructor has no code and is totally empty!
    MovieRecord(const MovieRecord& orig);
    virtual ~MovieRecord();
    void getURL(std::string url);

    std::string getTitle();
    std::vector<std::string> getDirector();
    std::vector<std::string> getCast();
    std::string getDateOfRelease();
    std::string getSynopsis();
    std::string getRunningTime();

    void setTitle(std::string titleStr);
    void setDirector(std::vector<std::string> directorStr);
    void setCast(std::vector<std::string> castStr);
    void setDateOfRelease(std::string DateOfReleaseStr);
    void setSynopsis(std::string SynopsisStr);
    void setRunningTime(std::string RunningTimeStr);

private:
    std::string title;
    std::vector<std::string> director;
    std::vector<std::string> cast;
    std::string dateOfRelease;
    std::string synopsis;
    std::string RunningTime;
};


When passing in the mr object into MovieDB, the whole thing becomes empty. Please kindly assist to see what is wrong?

I am aware that, instead of passing in a MovieRecord mr object to MovieDB, I can split up the object's 6 private variables and pass them in individually, but it seems kind of non-OO to me.

Could it be the fact that the variables in any MovieRecord is only populated via getURL() function?

If yes, please kindly suggest any ways that I can do it.
You should probably pass by reference, than by value.

Instead of:
void MovieDB::addMovie(MovieRecord mr)

you should have:
void MovieDB::addMovie(const MovieRecord &mr)
kbw is correct and that will avoid your problem, though I'm going to guess that the
root cause of your problem is an incorrectly coded copy constructor for MovieRecord
(since you declared one, you must implement it, and I'm guessing yours is empty?)
Problem solved. For future people, this is an example:

1
2
3
4
5
MovieRecord::MovieRecord(const MovieRecord& orig) 
{
      title = orig.title;
      RunningTime = orig.RunningTime;
}


Last edited on
If MovieRecord shouldn't be copied, disable it, otherwise fix it.
A better implementation of the above (none is technically needed since the default copy constructor provided
by the compiler does the same thing):

1
2
3
4
5
6
7
8
9
// Use of initializer lists  instead of assignment is recommended.
// Members should be initialized in the order of declaration (the
// compiler will reorder them anyway.  A reasonable compiler
// will give a warning if you don't put them in the right order)
MovieRecord::MovieRecord( const MovieRecord& r ) :
    title( r.title ), director(), cast(), dateOfRelease(),
    synopsis(), RunningTime( r.RunningTime ) 
{
}

Thanks to everyone for your kind help. Really learnt loads.

Sorry I am not used to C++ programming and hence did not know the existence of copy constructors.
Topic archived. No new replies allowed.