Bear with me on this one.. I've been following the Google Education C++ course and am currently working on the Database Project. The idea of the program is that we create a database object that in turn holds an array of composer objects. These composer objects contain attributes and information accessed via setters and getters. We're given header files along with two test implementation files, and we're expected to implement the header definitions.
The issue is, and please correct me if I'm wrong, we are asked to define the following header declaration:
Composer& AddComposer(string in_first_name, string in_last_name,
string in_genre, int in_yob, string in_fact);
and this involves creating a Composer object locally within the function, adding it too an array of Composers and then returning a reference that that single Composer object. From what I understand the local Composer object is deallocated upon function return and the reference would refer to nothing essentially. My first question is, can a Composer object created within AddComposer be returned as a reference and if so should it be?
My implementation for AddComposer is as follows:
Composer& Database::AddComposer(string in_first_name,
string in_last_name,
string in_genre,
int in_yob,
string in_fact) {
// Creating a new composer object
Composer composer;
composer.set_first_name(in_first_name);
composer.set_last_name(in_last_name);
composer.set_composer_yob(in_yob);
composer.set_composer_genre(in_genre);
composer.set_fact(in_fact);
// Adding the newly created composer object to the composers_ array
composers_[next_slot_] = composer;
// Increment the next_slot_ counter
next_slot_ ++;
return composers_[(next_slot_-1)];
}
I have instead tried to return the object from the composers_ array as its globally defined. This codel stil produces the error:
C:\Users\jprestid\AppData\Local\Temp\ccnMv1rz.o:test_database.cpp:(.text+0x1a8): undefined reference to `Composer::Promote(int)'
c:/Program Files/mingw-w64/x86_64-4.9.2-posix-seh-rt_v3-rev1/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\jprestid\AppData\Local\Temp\ccnMv1rz.o: bad reloc address 0x10 in section `.xdata'
collect2.exe: error: ld returned 1 exit status
Promote() is the first function called on the Composer object returned from the AddComposer() function.
My code base is online at Github and any help would be greatly appreciated. This problem has been stumping me for the whole day! Sorry for the size of my post and thanks again,
Jarvis
Edit: This was a compilation problem, I wasn't including the necessary file composer.cpp as Macro A corrected pointed out. I feel silly. Thanks everyone.