Cannot create an object from a class i created. Help!

W

Willson

Ok, first off I am learning C++ for the first time. I know Java very
well, but a new class of mine requires C++. So bare with me please.

I have 2 classes, Birthday.cpp and BirthdayParadox.cpp

Birthday is a class and Birthday paradox is mainly the driver for it. I
am trying to create an object of Birthday in BirthdayParadox and it is
not working.
I get the error: "'Birthday' undeclared (first use this function)"

any idea?



Birthday.cpp
---------------------------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

// #include any header files you need here
class Birthday
{
public:

int bDay;



// standard constructor
// default to January 1st if user provides invalid data
Birthday::Birthday(int doy)
{
// define the implementation code of the constructor
if ( doy < 1 && doy > 365 )
{
bDay = 1;
}
else
bDay = doy;


}

// Default constructor which generates a random birthday
// between January 1st and December 31. We assume only non-leap years

Birthday::Birthday()
{

bDay = setDay();

}

// Set birthday randomly
int Birthday::setDay()
{
srand((unsigned)time(0));
int random_integer = rand();

for(int index=0; index<1; index++)
{
random_integer = (rand()%365)+1;
}

return random_integer;
}

// Test for birthday equality
bool Birthday::eek:perator==( const Birthday &b)const
{

// fill in your code here
return true;

}

/*// print birthday information
std::eek:stream& operator<<(std::eek:stream& out, const Birthday &b)
{

// fill in your code here
cout>> "test";
} */
};





BirthdayParadox----------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>


using namespace std;

// #include any other necessary header files here
// also the required "using" statements

void testBirthdayParadox()
{ // Birthday Paradox test function

Birthday data; // <-------------------------------------------
this is where i get the error.
//srand( time(0) ) ;



// fill in your code here



}

int main()
{
char c;

//testBirthdayParadox();

cin >> c; // to force the DOS screen to stay until you press any
character

return EXIT_SUCCESS; // successful execution
}
 
A

amirkam1

I have 2 classes, Birthday.cpp and BirthdayParadox.cpp
I do not see two classes here.
There is only one class in Birthday.cpp.
You are getting this error because the BirthDay class is not available
for the BirthDayParadox.cpp compilation unit. (Every cpp file is
converted into a .obj as a separate compilation unit.) So you should
include the file declaring the Birthday class in this file.

I'll suggest you should have two separate files,

Birthday.h file as

#ifndef __BIRTHDAY__
#defeine __BIRTHDAY__
//Incldue whatever is required

class BirthDay
{
public:
// Declare Funtions and members
};
#endif //__BIRTHDAY__
------------------------------------------------------
Second file. BirthDay.cpp
#include "BirthDay.h"
// Include other files required

//Implement the functions here.
Birthday::Birthday(int doy)
{
// define the implementation code of the constructor
if ( doy < 1 && doy > 365 )
{
bDay = 1;
}
else
bDay = doy;


}
-----------------------------------------------

The paradox file will ermain the same as it is with addition of
following

#include "BirthDay.h"


regards
Amir Kamerkar
 
G

Guest

Willson said:
Ok, first off I am learning C++ for the first time. I know Java very
well, but a new class of mine requires C++. So bare with me please.

I have 2 classes, Birthday.cpp and BirthdayParadox.cpp

Birthday is a class and Birthday paradox is mainly the driver for it. I
am trying to create an object of Birthday in BirthdayParadox and it is
not working.
I get the error: "'Birthday' undeclared (first use this function)"

any idea?



Birthday.cpp
---------------------------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

// #include any header files you need here
class Birthday
{
public:

int bDay;



// standard constructor
// default to January 1st if user provides invalid data
Birthday::Birthday(int doy)
{
// define the implementation code of the constructor
if ( doy < 1 && doy > 365 )
{
bDay = 1;
}
else
bDay = doy;


}

// Default constructor which generates a random birthday
// between January 1st and December 31. We assume only non-leap years

Birthday::Birthday()
{

bDay = setDay();

}

// Set birthday randomly
int Birthday::setDay()
{
srand((unsigned)time(0));
int random_integer = rand();

for(int index=0; index<1; index++)
{
random_integer = (rand()%365)+1;
}

return random_integer;
}

// Test for birthday equality
bool Birthday::eek:perator==( const Birthday &b)const
{

// fill in your code here
return true;

}

/*// print birthday information
std::eek:stream& operator<<(std::eek:stream& out, const Birthday &b)
{

// fill in your code here
cout>> "test";
} */
};





BirthdayParadox----------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>


using namespace std;

// #include any other necessary header files here
// also the required "using" statements

void testBirthdayParadox()
{ // Birthday Paradox test function

Birthday data; // <-------------------------------------------
this is where i get the error.
//srand( time(0) ) ;



// fill in your code here



}

int main()
{
char c;

//testBirthdayParadox();

cin >> c; // to force the DOS screen to stay until you press any
character

return EXIT_SUCCESS; // successful execution
}
please add header file.
like this:
BirthdayParadox----------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
//add header file
#include "Birthday.cpp"
 
N

netmage88

don't you need to include a header file of birthday in your
BirthdayParadox??
I'm still a newbie so i might be wrong...hope this helps you.
 
W

Willson

thanks for the replies, I will have to research what header files are
and do, but hopefully that will solve the problem. I didnt know C++
needed a header file
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top