Hi
I'm new to C++ programming (although I have programmed in other languages) and am just trying to teach myself how to do basic classes. I have some very simple code which asks users for input on their favourite and worst CDs and stores these in two objects of class CD (which I've created). Now I want to add some extra functions to my main program so have declared the favourite and worst CDs as global variables but when I do, I'm getting the following error....
CDClassTest.obj : error LNK2001: unresolved external symbol "public: __thiscall CD::CD(void)" (??0CD@@QAE@XZ)
Debug/ClassExperiments.exe
: fatal error LNK1120: 1 unresolved externals
My code is as below....
#include <iostream>
#include <string>
#include "CD.H"
using namespace std;
CD myFavouriteCD; //global declarations of myFavouriteCD and myWorstCD so that other functions can use them
CD myWorstCD;
string getNameOfArtist()
{
string inputArtist;
cout<<"\nPlease enter name of artist: ";
getline(cin, inputArtist);
return inputArtist;
}
string getNameOfAlbum()
{
string inputAlbum;
cout<<"\nPlease enter title of album: ";
getline(cin, inputAlbum);
return inputAlbum;
}
int getNoOfSongs()
{
int inputNoOfTracks;
cout<<"\nPlease enter number of tracks : ";
cin>>inputNoOfTracks;
return inputNoOfTracks;
}
int getYearOfAlbum()
{
int inputYear;
cout<<"\nPlease enter year of album : ";
cin>>inputYear;
return inputYear;
}
void modifyFavouriteCD()
{
//need to modify so that it repeats the question until it gets a valid answer
int fieldToChange;
cout<<"Which field do you wish to change? Press 1 for artist, 2 for album name, 3 for number of tracks and 4 for year of album : ";
cin>>fieldToChange;
switch(fieldToChange)
{
case 1:
myFavouriteCD.setArtist(ge
tNameOfArt
ist());
break;
case 2:
myFavouriteCD.setTitle(get
NameOfAlbu
m());
break;
case 3:
myFavouriteCD.setNoOfTrack
s(getNoOfS
ongs());
break;
case 4:
myFavouriteCD.setYear(getY
earOfAlbum
());
break;
default:
cout<<"Not a valid choice";
}
}
void modifyWorstCD()
{
//need to modify so that it repeats the question until it gets a valid answer
int fieldToChange;
cout<<"Which field do you wish to change? Press 1 for artist, 2 for album name, 3 for number of tracks and 4 for year of album : ";
cin>>fieldToChange;
switch(fieldToChange)
{
case 1:
myWorstCD.setArtist(getNam
eOfArtist(
));
break;
case 2:
myWorstCD.setTitle(getName
OfAlbum())
;
break;
case 3:
myWorstCD.setNoOfTracks(ge
tNoOfSongs
());
break;
case 4:
myWorstCD.setYear(getYearO
fAlbum());
break;
default:
cout<<"Not a valid choice";
}
}
void modify()
{
//need to modify so that it repeats the question until it gets a valid answer
int modifyChoice =0;
cout<<"Which CD do you wish to modify? Press 1 for favourite or 2 for worst : ";
cin>>modifyChoice;
switch(modifyChoice)
{
case 1:
modifyFavouriteCD();
break;
case 2:
modifyWorstCD();
break;
default:
cout<<"Not a valid choice";
}
}
void main()
{
//get input for first class
cout<<"Favourite album \n";
cout<<"=============== \n \n";
//feed first class values
myFavouriteCD.setArtist(ge
tNameOfArt
ist());
myFavouriteCD.setTitle(get
NameOfAlbu
m());
myFavouriteCD.setNoOfTrack
s(getNoOfS
ongs());
myFavouriteCD.setYear(getY
earOfAlbum
());
cin.ignore(); //need to find out why we need this one!!!
//get input for second class
cout<<"\n \nWorst album \n";
cout<<"=========== \n \n";
//feed second class values
myWorstCD.setArtist(getNam
eOfArtist(
));
myWorstCD.setTitle(getName
OfAlbum())
;
myWorstCD.setNoOfTracks(ge
tNoOfSongs
());
myWorstCD.setYear(getYearO
fAlbum());
//print out values of first class
cout<<"\n \n Favourite album";
cout<<"\n Artist : "<<myFavouriteCD.getArtist
();
cout<<"\n Title : "<<myFavouriteCD.getTitle(
);
cout<<"\n Number of tracks : "<<myFavouriteCD.getNoOfTr
acks();
cout<<"\n Year : "<<myFavouriteCD.getYear()
;
//print out values of second class
cout<<"\n \n Worst album";
cout<<"\n Artist : "<<myWorstCD.getArtist();
cout<<"\n Title : "<<myWorstCD.getTitle();
cout<<"\n Number of tracks : "<<myWorstCD.getNoOfTracks
();
cout<<"\n Year : "<<myWorstCD.getYear()<<"\
n";
char modifyChoice;
cout<<"\n Do you wish to modify any of the details? (Y/N) : ";
cin>>modifyChoice;
if (modifyChoice == 'Y' || modifyChoice =='y')
{
modify();
}
}
Do I need to add a class definition to this file and if so, how and where does it goes?
Thanks!
Start Free Trial