I'm trying to pass an object userArtist into the constructor of a different class Artwork and instead of passing that object along it almost seems to call the default constructor of class Artist.
These are my files:
main.cpp
#include "Artist.h"
#include "Artwork.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
string userTitle, userArtistName;
int yearCreated, userBirthYear, userDeathYear;
getline(cin, userArtistName);
cin >> userBirthYear;
cin.ignore();
cin >> userDeathYear;
cin.ignore();
getline(cin, userTitle);
cin >> yearCreated;
cin.ignore();
Artist userArtist = Artist(userArtistName, userBirthYear, userDeathYear);
Artwork newArtwork = Artwork(userTitle, yearCreated, userArtist);
newArtwork.PrintInfo();
}
Artist.h
#ifndef ARTISTH
#define ARTISTH
#include <iostream>
#include <string>
using namespace std;
class Artist{
public:
Artist();
Artist(string artistName, int birthYear, int deathYear);
string GetName() const;
int GetBirthYear() const;
int GetDeathYear() const;
void PrintInfo() const;
private:
// TODO: Declare private data members - artistName, birthYear, deathYear
string artistName;
int birthYear;
int deathYear;
};
#endif
Artist.cpp
#include "Artist.h"
#include <iostream>
#include <string>
using namespace std;
/*Artist::Artist()
{
artistName = "unknown";
birthYear = -1;
deathYear = -1;
}
*/
Artist::Artist(string artistName, int birthYear, int deathYear)
{
artistName = artistName;
birthYear = birthYear;
deathYear = deathYear;
}
string Artist::GetName() const
{
return artistName;
}
int Artist::GetDeathYear() const
{
return deathYear;
}
int Artist::GetBirthYear() const
{
return birthYear;
}
void Artist::PrintInfo() const
{
cout << "Artist : " << artistName << " (";
if (birthYear < 0)
{
cout << "unknown)";
}
else if (deathYear < 0)
{
cout << birthYear << " to present)";
}
else
{
cout << birthYear << " to " << deathYear << "0";
}
}
Artwork.h
#ifndef ARTWORKH
#define ARTWORKH
#include <iostream>
#include <string>
using namespace std;
#include "Artist.h"
class Artwork{
public:
Artwork();
Artwork(string title, int yearCreated, Artist userArtist);
string GetTitle();
int GetYearCreated();
void PrintInfo();
private:
string title;
int yearCreated;
// TODO: Declare private data members - title, yearCreated
Artist userArtist;
// TODO: Declare private data member artist of type Artist
};
#endif
Artwork.cpp
#include "Artwork.h"
#include "Artist.h"
#include <iostream>
#include <string>
using namespace std;
Artwork::Artwork(string title, int yearCreated, Artist userArtist)
{
title = title;
yearCreated = yearCreated;
userArtist = userArtist;
}
// TODO: Define second constructor to initialize
// private fields (title, yearCreated, artist)
// TODO: Define get functions: GetTitle(), GetYearCreated()
void Artwork::PrintInfo()
{
cout << userArtist.GetName();
}
// Call the PrintInfo() function in the Artist class to print an artist's information
When I removed the default constructor of class Artist the compiler threw this message:
/usr/bin/ld: /tmp/cc6JxxRm.o: in function >Artwork::Artwork(std::__cxx11::basic_string<char, std::char_traits, std::allocator >, int, Artist)': Artwork.cpp:(.text+0x34): undefined reference to
Artist::Artist()' collect2: error: ld returned 1 exit status
It's almost as if the Artwork class can't recognize that userArtist already exists.