unresolved external symbol

Posted on 2006-03-29
Last Modified: 2010-04-01

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
            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;

            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: ";

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

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

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

      //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: ";

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

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

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

      //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?
Question by:Uzibat
    LVL 37

    Expert Comment

    by:Harisha M G
    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;    

    LVL 20

    Accepted Solution


    the constructor implementation for CD is missing:


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


    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 :)

    Author Comment

    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! :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now