-1

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.

Jarvis Prestidge
  • 326
  • 1
  • 10
  • Please add some information about the Composer object (does it have a copy constructor? what are its members?). Additionally please add your definition/initialization of the composers_ array – Daniel Apr 20 '15 at 09:46
  • The error you show have nothing to do with the code you show, it says that the function `Composer::Promote` haven't been defined anywhere. – Some programmer dude Apr 20 '15 at 09:47
  • Please post the compilation command too – Marco A. Apr 20 '15 at 09:48
  • As for your question about returning a local variable as a reference, you acre actually *not* returning a reference to a local variable, you are returning a reference to `composers_[(next_slot_-1)]`, and as i assume that `composers_` is a member variable then the reference will be valid as long as the `Database` object instance is valid – Some programmer dude Apr 20 '15 at 09:48
  • @Daniel my code is in the [Github](https://github.com/JarvisPrestidge/CPP-Practice-Code/tree/master/Database%20Project) link provided in the question. I didn't want the question to be too big. – Jarvis Prestidge Apr 20 '15 at 09:50
  • @Macrco A. compilation command is: $ g++ -std=c++14 -Wall test_database.cpp database.cpp – Jarvis Prestidge Apr 20 '15 at 09:51
  • @Jarvis I know. But this is StackOverflow not a "Code Review Forum". So please post all the relevant code here so everybody can have a short look at it instead of looking it up all over the internet (the point is: I'd need to go to GitHub, find the relevant files, find the relevant position within these files, ...) – Daniel Apr 20 '15 at 09:53
  • Is there a source file containing the definition of the `Composer` functions (e.g. `composer.cpp` or similar)? Then you need to build with that too. – Some programmer dude Apr 20 '15 at 09:53
  • I ran your code on a windows machine with mingw64 -> compiles just fine. I used `g++ test_database.cpp database.cpp composer.cpp -o test`. Double check you're also including the composer.cpp file for the translation unit to be correctly assembled – Marco A. Apr 20 '15 at 09:55
  • 1
    You seem to have 2 unrelated questions. Please make 2 separate posts. – Benjamin Lindley Apr 20 '15 at 09:55
  • @JoachimPileborg ahh, thank you for that. That is indeed the reason I'm trying to return the object via the array and not on its own. It is a member variable and my question isn't all that clear, but I meant to say I initially tried just returning the composer object alone. – Jarvis Prestidge Apr 20 '15 at 09:55
  • @MarcoA. oh gawd.. thats obviously the reason, you're right I haven't included all the necessary files for compilation. Geez louise sorry – Jarvis Prestidge Apr 20 '15 at 09:57
  • @JarvisPrestidge Glad we straightened that out. I'll post the answer to the issue so we can close this. – Marco A. Apr 20 '15 at 10:03

1 Answers1

1

I downloaded your code and compiled it with mingw64 on a Windows machine: it works fine.

The undefined reference (as confirmed in the comments) is caused by the fact that you're not including all the cpp files in the compilation line.

g++ test_database.cpp database.cpp composer.cpp -o test

Marco A.
  • 43,032
  • 26
  • 132
  • 246