GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Mar-2007, 07:48
wmbest2 wmbest2 is offline
New Member
 
Join Date: Mar 2007
Posts: 2
wmbest2 is on a distinguished road

Problem with using classes as Parameters and Return Types


Hello all.. I have a project in my Data Structures class in which I have to read in the IMDB data and eventually (Next Project) be able to solve the kevin bacon problem. Anyways I have been setting it up so that there are 5 Classes as follows:

Actor
Movie
MovieDB
ActorDB
DB

These contain all the information needed to solve the problem. However, I have become hung up on a problem with my code compiling and cant seem to find the error. I will post the Movie and Actor class. Both at some point use each other's class as a Parameter type. I am using MinGW and Eclipse.

CPP / C++ / C Code:
/**
 * @name Actor
 * @brief A Class dedicated to holding information about an actor
 */ 

#ifndef ACTOR_H_
#define ACTOR_H_

#include <string>
#include <vector>
#include "Movie.h"
#include "MovieDB.h"
#include "ActorDB.h"


class Actor
{
	public:
		
		/**
		 *  @brief This is the default constructor for class Actor
		 * 
		 * 	Default Constructor
		 * 
		 *  This constructor creates a new instance of Actor
		 */
		Actor();
		
		/**
		 *  @brief This is the default constructor for class Actor
		 * 
		 * 	Constructor
		 * 
		 *  This constructor creates a new instance of Actor based on the inputed name
		 */
		Actor(std::string nameIn);
		
		/**
		 *  @brief This is the destructor for class Actor
		 * 
		 * 	Destructor
		 * 
		 *  This is called when a instance of Actor is out of scope or deleted
		 */
		~Actor();
		
		/**
		 *  @brief Returns the name
		 *  @return std::string name
		 *  
		 *  This function returns the Actor's name
		 */
		void getName();
		
		/**
		 *  @brief Adds a movie to the actors list of movies
		 *  @param m An object of type Movie
		 *  
		 *  This function adds a movie tag pointer to the vector movies
		 */
		void addMovie(const Movie& m);
		
		
	private:
		
		// contains the actors name
		std::string name;
		// contains a list of the actor's movies
		std::vector<std::string*> movies;
		
};

#endif /*ACTOR_H_*/

CPP / C++ / C Code:
/**
 * @name Movie
 * @brief A Class dedicated to holding information about a movie
 */
#ifndef MOVIE_H_
#define MOVIE_H_

#include "Actor.h"
#include <string>

class Movie
{
	public:
		/**
		 *  @brief This is the default constructor for class Movie
		 * 
		 * 	Default Constructor
		 * 
		 *  This constructor creates a new instance of Movie
		 */
		Movie();
		
		/**
		 *  @brief This is the default constructor for class Movie
		 * 
		 * 	Constructor
		 * 
		 *  This constructor creates a new instance of Movie based on the inputed title and year
		 */
		Movie(std::string titleIn, int yearIn);
		
		/**
		 *  @brief This is the destructor for class Movie
		 * 
		 * 	Destructor
		 * 
		 *  This is called when a instance of Movie is out of scope or deleted
		 */
		~Movie();
		
		/**
		 *  @brief Returns the title
		 *  @return std::string title
		 *  
		 *  This function returns the Movie's Title
		 */
		std::string getTitle();
		
		/**
		 *  @brief Returns the year
		 *  @return int year
		 *  
		 *  This function returns the Movie's year
		 */
		int getYear();
		
		/**
		 *  @brief Adds an actors tag to the list
		 *  @param a An object of type actor
		 *  
		 *  This function adds an actors ActTag (string) to the movie so it can reference the ActorDB if needed
		 */
		std::string addActor(const Actor& a);
	
	private:
		// contains the title of the movie
		std::string title;
		
		// contains the year of the movie
		int year;
		
		// contains a list of the actor's movies
		std::vector<std::string*> actors;
};

#endif /*MOVIE_H_*/

Both Errors Occur at the addMovie/addActor Functions and the errors both look similar and look like this

Code:
In file included from ../Movie.h:8, from ../MovieDB.h:11, from ../DB.h:9, from ../Main.cpp:1: ../Actor.h:61: error: `Movie' has not been declared ../Actor.h:61: error: expected `,' or `...' before '&' token ../Actor.h:61: error: ISO C++ forbids declaration of `Movie' with no type
  #2  
Old 13-Mar-2007, 08:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problem with using classes as Parameters and Return Types


Quote:
Originally Posted by wmbest2
Both Errors Occur at the addMovie/addActor Functions and the errors both look similar and look like this

Code:
In file included from ../Movie.h:8, from ../MovieDB.h:11, from ../DB.h:9, from ../Main.cpp:1: ../Actor.h:61: error: `Movie' has not been declared ../Actor.h:61: error: expected `,' or `...' before '&' token ../Actor.h:61: error: ISO C++ forbids declaration of `Movie' with no type

Put a forward reference to "Movie" in Actor.h, and put a forward reference to "Actor" in Movie.h:

CPP / C++ / C Code:
#ifndef ACTOR_H_
#define ACTOR_H_

#include <string>
#include <vector>
#include "MovieDB.h"
#include "ActorDB.h"

class Movie;

class Actor
{
.
.
.};

CPP / C++ / C Code:
#ifndef MOVIE_H_
#define MOVIE_H_

#include <string>

class Actor;

class Movie
{
.
.
.
};

That way, no matter which is #included first, the compiler always knows what to do. Note that when the compiler is reading a header compiler doesn't need to know any details of the other class; it just needs to know that is a class. (Like "previews of coming attractions" at the movies.)

Regards,

Dave
  #3  
Old 13-Mar-2007, 12:49
darelf darelf is offline
Junior Member
 
Join Date: Feb 2007
Posts: 68
darelf will become famous soon enough

Re: Problem with using classes as Parameters and Return Types


Quote:
Originally Posted by davekw7x
That way, no matter which is #included first, the compiler always knows what to do. Note that when the compiler is reading a header compiler doesn't need to know any details of the other class; it just needs to know that is a class. (Like "previews of coming attractions" at the movies.)

Regards,

Dave

I would only add to this that, if you declare something by value, let's say if you stored an Actor object by value as a member of the Movie object (i.e. you have a member that is declared "Actor theStar;", then the compiler would need the full definition of Actor and not just a forward reference. The reason would be that it needs to allocate storage. When you only use pointers/references ( as you are doing ), then you don't have to allocate storage, and so the compiler doesn't need to know the details of the class.
  #4  
Old 13-Mar-2007, 20:08
wmbest2 wmbest2 is offline
New Member
 
Join Date: Mar 2007
Posts: 2
wmbest2 is on a distinguished road

Re: Problem with using classes as Parameters and Return Types


I apologize for the late response but the answer given solved my problem thanks.
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
a tester class and then some. postage Java Forum 1 06-May-2006 15:48
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
Bounded Buffering? pablowablo C Programming Language 0 17-Jan-2005 05:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.