We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

unresolved external symbol

Uzibat
Uzibat asked
on
Medium Priority
370 Views
Last Modified: 2010-04-01
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?
Comment
Watch Question

Harisha M GEngineer
CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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! :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.