-1

EDIT

Here is my new code:

class LibItem
{
public:
    //LibItem();
    //LibItem(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus)
    //{
    //  Title = setItemTitle;
    //  Author = setItemAuthor;
    //  ReleaseDate = setItemReleaseDate;
    //  Copyright = setItemCopyright;
    //  Genre = setItemGenre;
    //  Status = setItemStatus;
    //}
    //~LibItem(); //DO ******************
    virtual void PrintDetails() = 0;
    void setDetails(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus)
    {
        Title = setItemTitle;
        Author = setItemAuthor;
        ReleaseDate = setItemReleaseDate;
        Copyright = setItemCopyright;
        Genre = setItemGenre;
        Status = setItemStatus;
    }
    void setTitle(string TitleName)
    {
        Title = TitleName;
    }
    string getTitle()
    {
        return Title;
    }
    void setReleaseDate(string date)
    {
        ReleaseDate = date;
    }
    string getReleaseDate()
    {
        return ReleaseDate;
    }
    void setAuthor(string AuthorName)
    {
        Author = AuthorName;
    }
    string getAuthor()
    {
        return Author;
    }
    void setCopyright(string CopyrightDetails)
    {
        Copyright = CopyrightDetails;
    }
    string getCopyright()
    {
        return Copyright;
    }
    void setGenre(string GenreDetails)
    {
        Genre = GenreDetails;
    }
    string getGenre()
    {
        return Genre;
    }
    void setStatus(string StatusDetails)
    {
        Status = StatusDetails;
    }
    string getStatus()
    {
        return Status;
    }
private:
    string Title;
    string ReleaseDate;
    string Author;
    string Copyright;
    string Genre;
    string Status;
};

class Book : public LibItem
{
public:
    Book(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus, string setItemISBN)
    {
        setDetails(setItemTitle, setItemAuthor, setItemReleaseDate, setItemCopyright, setItemGenre, setItemStatus);
        setISBN(setItemISBN);
    }
    void setISBN(string ISBNDetails)
    {
        ISBN = ISBNDetails;
    }
    string getISBN()
    {
        return ISBN;
    }
    void PrintDetails()
    {
        cout << "Title: " << getTitle() << endl;
        cout << "Author: " << getAuthor() << endl;
        cout << "Release Date: " << getReleaseDate() << endl;
        cout << "Copyrite: " << getCopyright() << endl;
        cout << "Genre: " << getGenre() << endl;
        cout << "Status: " << getStatus() << endl;
        cout << "ISBN: " << getISBN() << endl;
    }

private:
    Book();
    string ISBN;

};

class DVD : public LibItem
{
public:
    DVD(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus, int setItemRunningTime, string setItemDirector, string setItemStudio, string setItemProducer)
    {
        setDetails(setItemTitle, setItemAuthor, setItemReleaseDate, setItemCopyright, setItemGenre, setItemStatus);
        setRunningTime(setItemRunningTime);
        setDirector(setItemDirector);
        setStudio(setItemStudio);
        setProducer(setItemProducer);
    }
    void setRunningTime(int RunningTimeDetails)
    {
        RunningTime = RunningTimeDetails;
    }
    int getRunningTime()
    {
        return RunningTime;
    }
    void setDirector(string DirectorDetails)
    {
        Director = DirectorDetails;
    }
    string getDirector()
    {
        return Director;
    }
    void setStudio(string StudioDetails)
    {
        Studio = StudioDetails;
    }
    string getStudio()
    {
        return Studio;
    }
    void setProducer(string ProducerDetails)
    {
        Producer = ProducerDetails;
    }
    string getProducer()
    {
        return Producer;
    }
    void PrintDetails()
    {
        cout << "Title: " << getTitle() << endl;
        cout << "Author: " << getAuthor() << endl;
        cout << "Release Date: " << getReleaseDate() << endl;
        cout << "Copyrite: " << getCopyright() << endl;
        cout << "Genre: " << getGenre() << endl;
        cout << "Status: " << getStatus() << endl;
        cout << "Running Time: " << getRunningTime() << endl;
        cout << "Director: " << getDirector() << endl;
        cout << "Studio: " << getStudio() << endl;
        cout << "Producer: " << getProducer() << endl;
    }

private:
    DVD();
    int RunningTime;
    string Director;
    string Studio;
    string Producer;

};

And this is my code to use the above classes:

LibItem *test;
test = new DVD("TestDVD","Test Author","01-01-2012","TestCopyright","TestGenre","TestStatus","120","TestDirector","TestStudio","TestProducer");
test->PrintDetails();

I am getting this error:

[BCC32 Error] Question 5.cpp(200): E2285 Could not find a match for 'DVD::DVD(const char *,const char *,const char *,const char *,const char *,const char *,const char *,const char *,const char *,const char *)'

Can I please have some information on why this error is happening and how to fix it?

Darryl Janecek
  • 399
  • 4
  • 9
  • 25

3 Answers3

0

Why is this?

You just declared a constructor LibItem() for your class but did not define it. Naturally, the linker cannot find it and complains of the same.
You need to define it as well.

LibItem()::LibItem()
{
}

Same is the case with the destructor.
Note that if you do not explictly declare a constructor or destructor for your class then compiler generates the definition of those implicitly, but if you explicitly declare these then the responsibility of providing the definition for them is yours.

Can someone please explain to me how to implement the classes in two separate files

Usually, in C++ the class definition go inside the header file(.h/.hpp) while the definitions of the member functions are placed in the source file(.cpp/.cc). Using the scope resoultion operator allows you to do so.

An Example:

//Myclass.h

class Myclass
{
    public:
       void doSomething();
};

//Myclass.cpp

void Myclass::doSomething()
{

}

Also, read about Namespaces, it is a good practice to place your custom classes in namespaces to avoid clash of symbol names.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Thanks. A question: What info should I place in the constructor and destructor? – Darryl Janecek Aug 29 '12 at 05:07
  • @DarrylJanecek The constructor intializes the object. You could create one that basically replicates your `setDetails()` function. The difference is that the constructor is invoked only once when the object is created, whereas `setDetails()` can be called at any time. You would need to change the declaration to accept parameters. – Code-Apprentice Aug 29 '12 at 05:11
  • @DarrylJanecek The constructor you have declared with no parameters is called a default constructor. In this case, you need to provide meaningful default values for all the member variables. If you decide that there are no meaningful default variables then don't createa default constructor at all. – Code-Apprentice Aug 29 '12 at 05:14
  • What about the destructor... in my above code, do I need one of these to remove anything important when the class is 'destructed'? Or there is no need in this case. – Darryl Janecek Aug 29 '12 at 05:53
  • @DarrylJanecek: No, Your class does not have any members who's lifetime needs to be explicitly managed(unlike if you have dynamically allocated pointerm members which will leak if you do not `delete` them explicitly).Do have a look at [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three).Also, pick up a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).You will need to read books to learn C++ you cannot learn it through programming forums. – Alok Save Aug 29 '12 at 05:56
  • I have updated my above post. Can I please have some help. Thanks. – Darryl Janecek Aug 29 '12 at 07:33
0

You have declared and defined all the functions for your class. On the other hand, you have only declared, but not defined the constructor. This is what the linker error is telling you.

Since you have asked about separating your code into .h and .cpp files, I will not go into any more detail about the linker error. Instead, I will show you how to get started separating your code.

First of all, I suggest that you create .h with only declarations. You can also include templates and inline functions. However, until you learn how these work, let's stick to the basics. I will illustrate with the first few functions of your LibItem class:

class LibItem
{
    public:
        LibItem();
        ~LibItem(); //DO ******************
        virtual void PrintDetails() = 0;
        void setDetails(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus);
        // The rest of your function declarations here.

    private:  
        string Title;  
        string ReleaseDate;  
        string Author;  
        string Copyright;  
        string Genre;  
        string Status;  
};

Notice that the class declaration only contains one line for each member function. This simply tells the compiler the function's name, return type, and parameter types. It does not give what the function actually does. That goes in the .cpp file as the function definition like this:

void LibItem::setDetails(string setItemTitle, string setItemAuthor, string setItemReleaseDate, string setItemCopyright, string setItemGenre, string setItemStatus)
{
    Title = setItemTitle;
    Author = setItemAuthor;
    ReleaseDate = setItemReleaseDate;
    Copyright = setItemCopyright;
    Genre = setItemGenre;
    Status = setItemStatus;
}

Notice how the class name and the "::" operator proceed the member function name. This tells the compiler that the setDetails() function is a member of the LibItem class.

Finally, you need to be able to compile and link all your code. The exact details depend on your environment. If you are using g++ on the command-line, you can compile your code with this command:

g++ main.cpp library.cpp

Assuming that you have a file named main.cpp with main() in it and that the code you posted in your OP is in a file named library.cpp.

As you begin creating more complex programs, you will want to use tools that help you manage your source files. make is a very powerful tool to do this. Many C++ IDEs use it under the hood. Talking about IDEs: these allow you to create "projects" which help you manage multiple source files and compile and link them into a single executable.

p.s. As a side note, you should use spaces to format your code rather than tabs. The later are not portable, especially to Q&A sites like this; they often create formatting different than what you see in your editor.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Your latest error is because your constructor for DVD takes 6 strings, 1 int then 3 more strings.

In passing

test = new DVD("TestDVD","Test Author","01-01-2012",
  "TestCopyright","TestGenre","TestStatus","120","TestDirector",
  "TestStudio","TestProducer");

You are giving 10 strings. "120" and 120 are very different things -- the first is a string which says 120, the second is an int with the value 120. C++ will not automatically convert between them.

Try this instead:

test = new DVD("TestDVD","Test Author","01-01-2012",
  "TestCopyright","TestGenre","TestStatus",120,"TestDirector",
  "TestStudio","TestProducer");
hcarver
  • 7,126
  • 4
  • 41
  • 67