[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

unresolved external symbol

Hi

I've just started learning C++ although I'm not new to programming so I've just started trying to teach myself the basics. I've written an incredibly simple program to input various details about a CD, store these in a class and output them again. The program compiles without any errors but when I go to build, I get the following message....

CompactDiscs.obj : error LNK2001: unresolved external symbol "public: __thiscall CD::CD(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::all
ocator<char> >,int,int)" (??0CD@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0HH@Z)
Debug/Assignment7.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

My code is as follows....

#include <iostream>
#include <string>

using namespace std;

class CD
{
      public:
            CD();    // Default Constructor
            CD(string artist, string title, int noOfTracks, int year);

            void setArtist(string newArtist)
            {
                  artist = newArtist;
            }
            string getArtist()
            {
                  return artist;
            }
            
            void setTitle(string newTitle)
            {
                  title = newTitle;
            }
            string getTitle()
            {
                  return title;
            }

            void setNoOfTracks(int newNoOfTracks)
            {
                  noOfTracks = newNoOfTracks;
            }
            int getNoOfTracks()
            {
                  return noOfTracks;
            }
            
            void setYear(int newYear)
            {
                  year = newYear;
            }
            int getYear()
            {
                  return year;
            }

      private:
            string artist;
            string title;
            int noOfTracks;
            int year;
};

void main()
{
      //define input holders
      string inputArtist;
      string inputTitle;
      int inputNoOfTracks;
      int inputYear;

      //get input for first class
      cout<<"Favourite album \n \n";

      cout<<"\n Please enter name of artist: ";
      cin>>inputArtist;

      cout<<"\n Please enter title of album : ";
      cin>>inputTitle;

      cout<<"\n Please enter number of tracks : ";
      cin>>inputNoOfTracks;

      cout<<"\n Please enter year of album : ";
      cin>>inputYear;

      //create class with values
      CD myFavouriteCD(inputArtist, inputTitle, inputNoOfTracks, inputYear);

      //get input for second class
      cout<<"\n \n Worst album \n \n";

      cout<<"\n Please enter name of artist: ";
      cin>>inputArtist;

      cout<<"\n Please enter title of album : ";
      cin>>inputTitle;

      cout<<"\n Please enter number of tracks : ";
      cin>>inputNoOfTracks;

      cout<<"\n Please enter year of album : ";
      cin>>inputYear;

      //create class with values
      CD myWorstCD(inputArtist, inputTitle, inputNoOfTracks, inputYear);

      //print out values of first class
      cout<<"\n \n Favourite album";

      cout<<"Artist : "<<myFavouriteCD.getArtist();
      cout<<"\n Title : "<<myFavouriteCD.getTitle();
      cout<<"\n Number of tracks : "<<myFavouriteCD.getNoOfTracks();
      cout<<"\n Year : "<<myFavouriteCD.getYear();

      //print out values of second class
      cout<<"\n \n Worst album";

      cout<<"Artist : "<<myWorstCD.getArtist();
      cout<<"\n Title : "<<myWorstCD.getTitle();
      cout<<"\n Number of tracks : "<<myWorstCD.getNoOfTracks();
      cout<<"\n Year : "<<myWorstCD.getYear();
}


What am I doing wrong?
0
Uzibat
Asked:
Uzibat
1 Solution
 
Harisha M GCommented:
Hi, where is your constructor definition ? Put this outside the class:

CD::CD(string artist, string title, int noOfTracks, int year)
{
    this->artist = artist;
    this->title = title;
    this->noOfTracks = noOfTracks;
    this->year = year;    
}


---
Harish
0
 
ikeworkCommented:
hi

the constructor implementation for CD is missing:

replace:

CD(string artist, string title, int noOfTracks, int year);

with:

CD(string a, string t, int n, int y) : artist(a), title(t), noOfTracks(n), year(y) {}


be sure to usedifferent names for passed parameters and member-variables, i changed them ...


good luck :)
ike
0
 
UzibatAuthor Commented:
Thanks a bunch. I'm literally trying to cobble together programs using a variety of websites' tutorials and haven't come across one that explains exactly what I need to know. Now I know how to make a proper constructor, life should be a lot easier!

Thanks for the prompt answers! :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now