[prog] Re: C++
wolf
wolf at wolfrising.net
Sun Mar 14 21:03:37 EST 2004
well it now compiles but returns this error:
Compiling...
ld: Undefined symbols:
_main
shell returned 1
also does anyone know how and where in the program to tell it to write
the information
to a file?
latest version :
#include <iostream>
#include <cstring>
#ifndef _CPP_OSTREAM
#define _CPP_OSTREAM
using namespace std;
class cd {
private:
string artist;
string title;
int year;
string genre;
public:
cd();
cd(string artist, string title, string genre, int year);
//void showCD();
//void saveCDLibrary();
//void printCDLibrary();
cd (string sartist, string stitle, string sgenre, int iyear) {
// create a new cd object
artist = sartist;
title = stitle;
genre = sgenre;
year = iyear;
}
};
cd::showCD() {
// print a cd object
cout << artist << " " << genre << " " << title << " " << year << endl;
}
cd::saveCDLibrary(cd cdLibrary[], int length) {
// need a fileIO stream of some sort.
for (int i; i < length; i++) {
cin << cdLibrary[i].showCD();
}
}
cd::printCDLibrary(cd cdLibrary[], int length) {
// This maybe needs sorted...
for (int i; i < length; i++) {
cout << cdLibrary[i].showCD();
}
}
void readCDLibrary(cd cdLibrary, int length) {
string input, artist, title, genre;
int year;
// Open the file ?
for (int i = 0; i < length; i++) {
inputstream >> input;
// artist, title, genre, year
cdLibrary[i] = new cd(artist, title, genre, year);
}
}
int main()
{
int CDcount = 0;
const int LIBRARY_SIZE = 10;
string artist, title, genre;
int year;
cd cdLibrary[LIBRARY_SIZE];
// Ask if they want to read the CDs from a file or enter new
// data:
cout << "Read from file (1) or Enter new data (0)? ";
cin >> ans;
if (ans) {
// read from the file?
readCDLibrary(cdLibrary, LIBRARY_SIZE);
} else {
// read in information about CDs
while (cdCount < LIBRARY_SIZE) {
cout << "Enter CD Information:" << endl;
cout << "Enter year: (0 to quit)";
cin >> year;
if (year == 0) { break; }
cout << "Enter artist: ";
cin >> artist;
cout << "Enter title: ";
cin >> title;
cout << "Enter genre: ";
cin >> genre;
cdLibrary[cdCount] = new cd(artist, title, genre, year);
cdCount++;
}
}
saveCDLibrary(cdLibrary, LIBRARY_SIZE);
printCDLibrary(cdLibrary, LIBRARY_SIZE);
return 0;
}
#endif
here is the original question I've been destroying : )
> write a simple program to track an individual's cd library, the
> program should permit the user to enter 10 cds. Each cd
> should contain the following items: artist, album title, year and
> genre. the program s! hould display all the cds in order
> by artist. design a class and represent each album as an object
> of that class. provide the capability to store the data
> in a text file and retrieve it again the next time the program is
> run.
On Mar 14, 2004, at 12:12 PM, dominik.schramm at gmxpro.net wrote:
> Hi,
>
> wolf <wolf at wolfrising.net> writes:
>
>> [...]
>> here are the error messages:
>> Compiling...
>> cd.cpp:20: error: return type specification for constructor invalid
>
> The contructor must have an *empty* type specification, not "void".
> See below.
>
>> cd.cpp:29: error: `void cd::showCD()' and `void cd::showCD()' cannot
>> be
>> overloaded
>
> See below: the function signatures are the same (thus overloading is
> impossible) but the compiler sees this function twice, once in a
> declaration,
> then in a declaration plus implementation.
>
>> cd.cpp:109: error: parse error at end of input
>
> Don't know about this. Maybe the missing semicolon? (see below)
>
>> this is line 20:
>> 20 void cd (string sartist, string stitle, string sgenre, int
>
> Leave away "void", should be:
> cd (string sartist, ...)
>
>> [...]
>> here's the current version of the program:
>> #include <iostream>
>> using namespace std;
>>
>> class cd {
>>
>> [...]
>>
>> private:
>> cd(string artist, string title, string genre, int year);
>> void showCD();
>
> This is the declaration for the showCD function.
>
>> [...]
>> void showCD() {
>> // print a cd object
>> cout << artist << " " << genre << " " << title << " " << year <<
>> endl;
>> }
>
> This is another (!) declaration plus implementation of showCD. Since
> the signature
> is the same in both cases, the compiler complains that you cannot do
> overloading
> this way.
>
> BTW. It's confusing to implement member functions inside the class
> declaration,
> I'd do this outside the class declaration, using names like cd::showCD
> etc.
> I think the above error is an example of such confusion.
>
>> [...]
>> void readCDLibrary(cd cdLibrary, int length) {
>> string input, artist, title, genre;
>> int year;
>>
>> // Open the file ?
>> for (int i = 0; i < length; i++) {
>> inputstream >> input;
>>
>> // artist, title, genre, year
>>
>> cdLibrary[i] = new cd(artist, title, genre, year);
>> }
>> }
>
> AFAIK there should be a ; after the class declaration.
>
>
> hope this helps,
> regards,
> dominik
>
> --
> Dominik Schramm <dominik.schramm at gmxpro.net>
> pgp key via http://www.cam.ac.uk.pgp.net/pgpnet/wwwkeys.html
>
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
>
More information about the Programming
mailing list