0

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.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 1
    Because you are not using the constructor initializer list to initialize objects in the `Artwork` constructor they must first be default initialized and then assigned in the body of the constructor. That's wrong too by the way, `title = title;` and the rest are assigning the parameter to itself. Use `this->variableName` to refer to the member variable when there is a name conflict. In the end though, since you said there would be a default constructor by adding it to the `Artist` class you must provide one, and you did not, hence the error. – Retired Ninja Dec 13 '22 at 02:50
  • Your C++ textbook should have multiple detailed chapters on how to use constructors in C++ to initialize class members. You might want to consider reading them, since this topic (and its many rules) will be covered extensively there, much more than can be explained in one or two paragraphs on Stackoverflow. Stackoverflow does not really work, very well, as a C++ textbook replacement. – Sam Varshavchik Dec 13 '22 at 02:53

0 Answers0