• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

How can I set up a date.h, date.cpp, person.h, person.cpp, and main.cpp C++ program?

I need to write a program with date.h, date.cpp, person.h, person.cpp, and a main.cpp file. The main.cpp will incorporate all the previous files.

I am trying to start small, using the following files for testing and troubleshooting purposes, but I ran into a lot of bugs I'm unable to resolve.  date.cpp date.h main.cpp

I need to use classes in my program.

Some of the error messages I get include:
expected namespace-name before ';' token
`<type error>' is not a namespace
`Date' undeclared (first use this function)

I also need to know if the header file should be in the same folder as the main.cpp program.

Thanks a lot!
0
talk2eric
Asked:
talk2eric
  • 13
  • 13
1 Solution
 
jkrCommented:
You forgot to add 'date.h' to 'main.cpp' - apart from that, it should work, e.g.
//  testapp.cpp

       #include <iostream>
       #include "date.h" // <--- missing
       using namespace std;
       using namespace Time;  // gives us access to Date class

       int main()
       {
       Date dob, marriageDate;

       dob.setDate(29, 2, 1984); // leapyear baby
       marriageDate.setDate(3, 9, 2005);
       dob.printDate;
       marriageDate.printDate;

       return 0;
       }

Open in new window


And yes, putting date.h in teh same folder would be the preferred and simplest way, that is unless you need the date class in other projects - then it would be worthwile considering putting it in s 'special' folder.
0
 
talk2ericAuthor Commented:
I inserted #include "date.h" which helped alot.

For dob.printDate; I'm getting the error message statement cannot resolve address of overloaded function. What does this mean?
0
 
talk2ericAuthor Commented:
Okay...I just figured out that it should be dob.printDate(); instead of dob.printDate;

Now I'm getting  [Linker error] undefined reference to `Time::Date::Date()'  error messages.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jkrCommented:
How are you compiling? If you are doing that on the command line, you need to specify all source files, e.g.

g++ -o testprog.exe main.cpp date.cpp person.cpp
0
 
talk2ericAuthor Commented:
I am using Dev-C++.
0
 
jkrCommented:
Hmmm, have you included all your source files in your project?
0
 
jkrCommented:
(or, if you are compiling on the command line, just substitute 'g++' with the name of your actual compiler)
0
 
talk2ericAuthor Commented:
I'm not sure what you are asking. I included date.cpp and date.h in the same folder as main.cpp.
0
 
jkrCommented:
So, how are you compiling? Just placing files in folders is not really enough ;o)
0
 
talk2ericAuthor Commented:
Okay...Please tell me what I need to do.
0
 
jkrCommented:
Well, I wrote that above already. What are you doing now? How are you compiling your code?
0
 
talk2ericAuthor Commented:
I am using Bloodshed Dev-C++ to compile the program, but get the linker error. I'm not sure what I am doing wrong.
0
 
talk2ericAuthor Commented:
I just click on the compile and run button to run main.cpp. I'm not sure of any other way to compile the program.
0
 
jkrCommented:
How did you add 'main.cpp'? Try adding the other source files the same way.
0
 
talk2ericAuthor Commented:
I'm trying to figure this part out. I'm researching compiling multiple files in Dev-C++.

Are the files supposed to be in a project folder?
0
 
jkrCommented:
Well, it's nicer and easier if they are, but in general, they don't have to. Adding the files to DevC++ just like you added main.cpp should work.
0
 
talk2ericAuthor Commented:
I tried clicking on the "rebuild all" button when main.cpp was open. It triggered and opened up date.cpp file. Now, I have to troubleshoot the date.cpp file.

I get the message that 'Date' has not been declared.
0
 
jkrCommented:
Well, you nee to add #include "date.h" to date.cpp as well, e.g.
//  Date.cpp - first letter is capitalized by tradition
       //  implementations of stuff in date.h

       #include "date.h" // <---- this here

       namespace Time  // tie the following code to the Time namespace
       {
       // the following constructor initializes each data member
       Date::Date()
       {
          day = 1;    // this and the following lines insure that Date objects
          month = 1;  // start in a consistent state
          year = 1900;
       }

       // the following destructor releases resources used by objects of type Date
       // when they no longer exist
       Date::~Date()
       {
       }

       void Date::setDate( ind d, int m, int y )
       {
             year = y; // validates that year is 1900 or greater
             month = m;
             day = d;
       } // end of function

       int Date::printDate()
       {
          // prints in American format with minimal formatting
          cout << month << "/" << day << "/" << year << endl;
       }
       } // end of namespace Time in Date.cpp

Open in new window

0
 
talk2ericAuthor Commented:
Once again, I also added #include "date.h" into date.cpp and this fixed the error about date not being declared.

Now, I am trying to resolve the error "variable or field `setDate' declared void."
0
 
jkrCommented:
Hm, 'setDate()' is in face declared as void, so that shuld be fine - what's the exact error?
0
 
talk2ericAuthor Commented:
variable or field `setDate' declared void   on line 22


Other errors for line 22 include
`int Time::Date::setDate' is not a static member of `class Time::Date'
`ind' was not declared in this scope
expected primary-expression before "int"

On line 23, I get
initializer expression list treated as compound expression
expected `,' or `;' before '{' token
member function `int Time::Date::printDate()':
0
 
jkrCommented:
Hmmm, small problem, big effect - in 'setDate()' the parameter name is misspelled . Instead of

void Date::setDate( ind d, int m, int y )

it should be

void Date::setDate( int d, int m, int y )

Also, you forgot to '#include <iostream>' for 'cout' and 'printDate()' needs to return a value. Make that
// date.h - name is lower case by tradition

       #ifndef _DATE_
       #define _DATE_

       namespace Time  // tie the following code to the Time namespace
       {
       class Date 
       {
       public:
          Date();  // this is a 'constructor' - more about that later
          ~Date(); // this is a 'destructor' - more about that later
          void setDate( int, int, int ); // method or function to set a date
          int printDate(); // method or function to get a date
       private:
          int day;   // data member of this class
          int month; // data member of this class
          int year;  // data member of this class

       };
       } // end of namespace Time in date.h
       #endif

Open in new window

#include "date.h"
	   #include <iostream>
	   using namespace std;
  //  Date.cpp - first letter is capitalized by tradition
       //  implementations of stuff in date.h

       namespace Time  // tie the following code to the Time namespace
       {
       // the following constructor initializes each data member
       Date::Date()
       {
          day = 1;    // this and the following lines insure that Date objects
          month = 1;  // start in a consistent state
          year = 1900;
       }

       // the following destructor releases resources used by objects of type Date
       // when they no longer exist
       Date::~Date()
       {
       }

       void Date::setDate( int d, int m, int y )
       {
             year = y; // validates that year is 1900 or greater
             month = m;
             day = d;
       } // end of function

       int Date::printDate()
       {
          // prints in American format with minimal formatting
          cout << month << "/" << day << "/" << year << endl;
		  return 0;
       }
       } // end of namespace Time in Date.cpp

Open in new window

#include "date.h"
       //  testapp.cpp

       #include <iostream>
       using namespace std;
       using namespace Time;  // gives us access to Date class

       int main()
       {
       Date dob, marriageDate;

       dob.setDate(29, 2, 1984); // leapyear baby
       marriageDate.setDate(3, 9, 2005);
       dob.printDate();
       marriageDate.printDate();

       return 0;
       }

Open in new window


At least that compiles for me.
0
 
jkrCommented:
Any further problems?
0
 
talk2ericAuthor Commented:
I think it is working so far.

I'm trying to get the console that opens up to display something. It is blank. This will give me some assurrance that it is working.

Next, I need to incorporate person.h and person.cpp files into the project.

Since date.cpp is a .cpp file, I am unsure of how it interacts with main.cpp.
0
 
jkrCommented:
It already does interact, otherwise you wouldn't be able to e.g. call

dob.setDate(29, 2, 1984);

from main.cpp.
0
 
talk2ericAuthor Commented:
I am using a Dev C++ programming environment, so it would have been very helpful to explain things from the point of view of my IDE. Otherwise, the information given is very helpful.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now