creating a header file problems

This is a discussion on creating a header file problems within the C++ Programming forums, part of the General Programming Boards category; ok i attempted to create a header file by copying exactly what was in the book...and i got some errors ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    creating a header file problems

    ok i attempted to create a header file by copying exactly what was in the book...and i got some errors so here is my header file and then my actual program using it:


    Code:
    class Date
    {
    public:
    	//intializes variables
    	Date();
    	//assigns program values to variables
    	void assigndate(char,char,char);
    	//returns formatted date
    	char getdate();
    private:
    	char month[50];
    	char day[50];
    	char year[50];
    };
    
    //implentation section
    Date::Date()
    {
    	//constructor
    	month = "";
    	day = "";
    	year = "";
    }//end of default constructor
    
    void Date::assigndate(char m[50],char d[50],char y[50])
    {
    	month = m;
    	day = d;
    	year = y;
    }//end of assigndate function
    
    char Date::getdate()
    {
    	return month + "/" + day + "/" + year;
    }// end of getdate function
    
    
    include <iostream>
    using namespace std;
    #include <Date.h>
    int main()
    {
    	//create object
    	Date dateobj;
    
    	//declare variables
    	char hiremonth[50] = "";
    	char hireday[50] = "";
    	char hireyear[50] = "";
    
    	//get month,day, and year
    	cout << "Enter the month: ";
    	cin.get(hiremonth,50);
    	cin.get();
    	cout << endl << "Enter the day: ";
    	cin.get(hireday,50);
    	cin.get();
    	cout << endl << "Enter the year: ";
    	cin.get(hireyear,50);
    	cin.get();
    
    	//set the date
    	dateobj.assigndate(hiremonth,hireday,hireyear);
    
    	//display the date
    	cout << "\nEMployee hire date " << dateobj.getdate() << endl;
    
    	return 0;
    }//end main function
    
    
    and now the errors:
    
    Compiling...
    CLASStest1.cpp
    c:\program files\microsoft visual studio\vc98\include\date.h(20) : error C2440: '=' : cannot convert from 'char [1]' to 'char [50]'
            There is no context in which this conversion is possible
    c:\program files\microsoft visual studio\vc98\include\date.h(21) : error C2440: '=' : cannot convert from 'char [1]' to 'char [50]'
            There is no context in which this conversion is possible
    c:\program files\microsoft visual studio\vc98\include\date.h(22) : error C2440: '=' : cannot convert from 'char [1]' to 'char [50]'
            There is no context in which this conversion is possible
    c:\program files\microsoft visual studio\vc98\include\date.h(26) : error C2511: 'assigndate' : overloaded member function 'void (char [],char [],char [])' not found in 'Date'
            c:\program files\microsoft visual studio\vc98\include\date.h(2) : see declaration of 'Date'
    c:\program files\microsoft visual studio\vc98\include\date.h(34) : error C2110: cannot add two pointers
    c:\documents and settings\evelyn\desktop\adam\c++\classtest1.cpp(26) : error C2664: 'assigndate' : cannot convert parameter 1 from 'char [50]' to 'char'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Error executing cl.exe.
    
    CLASStest1.obj - 6 error(s), 0 warning(s)
    ive never done this before so im not really sure what these errors mean any help would be appreciated thanx
    hooch

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    well the errors are pretty much self explanatory, but I will go through some below. And the header declaration is fine, it is what you are doing in the class itself that is causing most of the errors:

    Code:
    error C2511: 'assigndate' : overloaded member function 'void (char [],char [],char [])
    the declaration of assigndate is onyl set to take three chars and not char arrays so replace it with:
    Code:
    void assigndate(char*, char*, char*);
    also why are all your arrays 51 elements long? for month make it the number of longest month's spelling, make day 31, and year 4. Its really quite late so I'm not seeing to well now, I'll let you know more tomorrow morning.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    You cant copy a char array like this;
    Code:
      month = m;
      day = d;
      year = y;
    instead try
    Code:
    strcpy(month, m);
    strcpy(day, d);
    strcpy(year, y);
    strcpy

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    i appreciate the help but now i have this error:


    c:\program files\microsoft visual studio\vc98\include\date.h(26) : error C2511: 'assigndate' : overloaded member function 'void (char *,char *,char *)' not found in 'Date'
    c:\program files\microsoft visual studio\vc98\include\date.h(2) : see declaration of 'Date'


    which i have no clue what it means??
    hooch

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Could you please post the updated source code.

    Just making a wild guess, make sure your declaration of a method in the header file match the method you use in the source file. If that didnt make sense, take a look at this article

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    hmm well as far as i can tell that didnt seem to be the problem although it saids it is still so i dont know but here is the updated source code perhaps that will help(i dont think there about but a couple of changes in there from before but anyways herre it is):


    Code:
    //header file:
    
    class Date
    {
    public:
    	//intializes variables
    	Date();
    	//assigns program values to variables
    	void assigndate(char,char,char);
    	//returns formatted date
    	char getdate();
    private:
    	char month[50];
    	char day[50];
    	char year[50];
    };
    
    //implentation section
    Date::Date()
    {
    	//constructor
    	month = "";
    	day = "";
    	year = "";
    }//end of default constructor
    
    void Date::assigndate(char* ,char* ,char* )
    {
    	strcpy(month,m);
    	strcpy(day,d),
    	strcpy(year,y);
    }//end of assigndate function
    
    char Date::getdate()
    {
    	return month + "/" + day + "/" + year;
    }// end of getdate function
    
    //.cpp file
    
    
    #include <iostream>
    using namespace std;
    #include <Date.h>
    int main()
    {
    	//create object
    	Date dateobj;
    
    	//declare variables
    	char hiremonth[50] = "";
    	char hireday[50] = "";
    	char hireyear[50] = "";
    
    	//get month,day, and year
    	cout << "Enter the month: ";
    	cin.get(hiremonth,50);
    	cin.get();
    	cout << endl << "Enter the day: ";
    	cin.get(hireday,50);
    	cin.get();
    	cout << endl << "Enter the year: ";
    	cin.get(hireyear,50);
    	cin.get();
    
    	//set the date
    	dateobj.assigndate(hiremonth,hireday,hireyear);
    
    	//display the date
    	cout << "\nEMployee hire date " << dateobj.getdate() << endl;
    
    	return 0;
    }//end main function
    ok thats all thanx


    oops sorry about that lol
    Last edited by ssjnamek; 12-02-2003 at 11:44 AM.
    hooch

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    well you onyl changed the defintion of the function but not the decleration in the class! I think you should review passing arrays as parameters to functions.


    EDIT:: you placed a wrong slash in the code tags...please edit your last post accordingly

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Alot easier to read this :

    Code:
    class Date
    {
    public:
      Date();
      void assigndate(char,char,char); <--- see this declaration
      char getdate();
    private:
      char month[50];
      char day[50];
      char year[50];
    };
    
    
    Date:Date()
    {
      month = "";
      day = "";
      year = "";
    }
    
    void Date::assigndate(char* ,char* ,char* ) <-- why isn't this the same as the above
    {
      strcpy(month,m);
      strcpy(day,d),
      strcpy(year,y);
    }
    
    char Date::getdate()
    {
      return month + "/" + day + "/" + year;
    }
    
    
    #include <iostream>
    using namespace std;
    #include <Date.h>
    int main()
    {
      Date dateobj;
    
      char hiremonth[50] = "";
      char hireday[50] = "";
      char hireyear[50] = "";
    
      cout << "Enter the month: ";
      cin.get(hiremonth,50);
      cin.get();
      cout << endl << "Enter the day: ";
      cin.get(hireday,50);
      cin.get();
      cout << endl << "Enter the year: ";
      cin.get(hireyear,50);
      cin.get();
      dateobj.assigndate(hiremonth,hireday,hireyear);
    
      cout << "\nEMployee hire date " << dateobj.getdate() << endl;
    
      return 0;
    }
    Last edited by laasunde; 12-02-2003 at 11:51 AM.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    can u clarify that i still dont know all the computer terms to well but usually once i figure out what im doing i can get it done but never figure out the computer terms lol?

    EDIT:: oops sorry i was posting and didnt see ur last post so this one was in reference to the last post before that but thanx i see it now

    ok and i did more fixing and i got it d own to 1 error YAY lol ok here is the new version of it:


    Code:
    //the header file
    
    class Date
    {
    public:
    	//intializes variables
    	Date();
    	//assigns program values to variables
    	void assigndate(char*,char*,char*);
    	//returns formatted date
    	char getdate();
    private:
    	char* month;
    	char* day;
    	char* year;
    };
    
    //implentation section
    Date::Date()
    {
    	//constructor
    	month = "";
    	day = "";
    	year = "";
    }//end of default constructor
    
    void Date::assigndate(char* m ,char* d ,char* y)
    {
    	strcpy(month,m);
    	strcpy(day,d),
    	strcpy(year,y);
    }//end of assigndate function
    
    char Date::getdate()
    {
    	return month + "/" + day + "/" + year;
    }// end of getdate function
    
    
    //the .cpp file
    
    #include <iostream>
    using namespace std;
    #include <Date.h>
    int main()
    {
    	//create object
    	Date dateobj;
    
    	//declare variables
    	char hiremonth[50] = "";
    	char hireday[50] = "";
    	char hireyear[50] = "";
    
    	//get month,day, and year
    	cout << "Enter the month: ";
    	cin.get(hiremonth,50);
    	cin.get();
    	cout << endl << "Enter the day: ";
    	cin.get(hireday,50);
    	cin.get();
    	cout << endl << "Enter the year: ";
    	cin.get(hireyear,50);
    	cin.get();
    
    	//set the date
    	dateobj.assigndate(hiremonth,hireday,hireyear);
    
    	//display the date
    	cout << "\nEMployee hire date " << dateobj.getdate() << endl;
    
    	return 0;
    }//end main function
    
    // the error
    
    --------------------Configuration: CLASStest1 - Win32 Debug--------------------
    Compiling...
    CLASStest1.cpp
    c:\program files\microsoft visual studio\vc98\include\date.h(34) : error C2110: cannot add two pointers
    Error executing cl.exe.

    i dont really know much about pointers to well so thats probably why i dont understand this error i would think unless im wrong but im planning on looking that up relatively soon
    Last edited by ssjnamek; 12-02-2003 at 12:02 PM.
    hooch

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Main code:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include "Date.h"
    using namespace std;
    
    int main()
    {
      Date dateobj;
      string hiremonth;
      string hireday;
      string hireyear;
    
      cout << "Enter the month: ";
      getline(cin, hiremonth);
      
      cout << endl << "Enter the day: ";
      getline(cin,hireday);
    
      cout << endl << "Enter the year: ";
      getline(cin, hireyear);
    
      dateobj.assigndate(hiremonth,hireday,hireyear);
      cout << "\nEMployee hire date " << dateobj.getdate() << endl;
    
      system("PAUSE");	
      return 0;
    }
    date.h file
    Code:
    #include <string>
    using namespace std;
    class Date
    {
    public:
      Date();
      void assigndate(string, string, string);
      string getdate();
    private:
      string month;
      string day;
      string year;
    };
    date.cpp file
    Code:
    #include "date.h"
    
    Date::Date(){ }
    
    void Date::assigndate(string m ,string d ,string y)
    {
      month = m;
      day = d;
      year = y;
    }
    
    string Date::getdate()
    {
      return month + "/" + day + "/" + year;
    }
    This should work and you dont need to worry about pointers.

    Hope it helps.
    Last edited by laasunde; 12-02-2003 at 03:55 PM.

  11. #11
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    //implentation section
    Dateate()
    Honestly dude a smiley in your code??
    That's gotta be asking for it

  12. #12
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Originally posted by WDT
    Honestly dude a smiley in your code??
    That's gotta be asking for it
    to get rid of that just run off the smilies when you post any code.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    aside from the <string> header file changed which i dont mind that being changed except i dont have it on my compiler so ill jus go redo it later but the only difference i saw was the
    system("PAUSE")

    which i have no clue what that does...?
    hooch

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Here is an example withot string (just remove system("PAUSE"); it's a compiler spesific code bit).
    Code:
    #include <iostream>
    #include "Date.h"
    using namespace std;
    int main()
    {
      Date dateobj;
      char hiremonth[50] = "";
      char hireday[50] = "";
      char hireyear[50] = "";
    
      cout << "Enter the month: ";
      cin.get(hiremonth,50);
      cin.get();
      cout << endl << "Enter the day: ";
      cin.get(hireday,50);
      cin.get();
      cout << endl << "Enter the year: ";
      cin.get(hireyear,50);
      cin.get();
      
      dateobj.assigndate(hiremonth,hireday,hireyear);
      cout << "\nEmployee hire date " << dateobj.getdate() << endl;
      system("PAUSE");
      return 0;
    }
    Code:
    #include <string.h>
    using namespace std;
    
    class Date
    {
    public:
      Date();
      void assigndate(char[],char[],char[]);
      char* getdate();
    private:
      char month[10];
      char day[10];
      char year[10];
      char answer[50];
    };
    Code:
    #include "date.h"
    
    Date::Date() 
    {
      strcpy(month,"");
      strcpy(day,""),
      strcpy(year,"");
      strcpy(answer, "");
    }
    
    void Date::assigndate(char m[10] ,char d[10] ,char y[10])
    {
      strcpy(month,m);
      strcpy(day,d),
      strcpy(year,y);
    }
    
    char* Date::getdate()
    {
      strcat(answer, month);
      strcat(answer, "/");
      strcat(answer, day);
      strcat(answer, "/");
      strcat(answer, year);
      return answer;
    }
    There are probably alot better ways of doing this but this should atleast work. This is a good reference to manipulate c-strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Replies: 6
    Last Post: 04-02-2002, 04:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21