Thread: Need help get this working

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    45

    Need help get this working

    It's probably an easy program to fix but as a newbie I'm having trouble understanding the concept of class.

    I'm supposed to create a person class to represent a person. (You may call the class personType.) To simplify things, have the class have 2 variable members for the person's first and last name. Include 2 constructors. One should be a default constructor and the other should be one with parameters. Include respective functions for:
    setting the name,
    getting the name, and
    printing the name on the screen.

    This is what I did so far, but I'm having trouble running it.
    Code:
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;
    
    class personType
    {
    public:
        void print() const;    
        void setName(string first, string last); 
       void getName(string& first, string& last); 
       personType(string first, string last);  //Constructor with parameters
    
    private:
       string firstName; //store the first name    
       string lastName;  //store the last name};
    };
    int Main ()
    {
    	personType();   	 //Default constructor;
    	{
      	     string firstName; //store the first name    
    	    string lastName;  //store the last name
    	}
    	 void personType::print() 
    	 {
    	cout<<firstName<<" "<<lastName;
    	 }
                    void personType::setName(string first, string last)
                     {
    		firstName = first;
    		lastName = last;
                      }
    
    return 0;
    }
    If anyone can explain what I'm doing wrong I would greatly appreciate it.
    Thanks
    Last edited by flicka; 10-22-2005 at 08:55 PM.

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Ok! If I put this part:
    Code:
    #ifndef personType_H
    #define personType_H
    class personType
    {
    public:
        void print() const;    //Function to output the first name and last name
        void setName(string first, string last);  //Function to set firstName and lastName
    	void getName(string& first, string& last); 	    //Function to return firstName and lastName via the parameters	    
    	personType(string first, string last);	    //Constructor with parameters
    private:
       string firstName; //store the first name    
       string lastName;  //store the last name};
    };
    #endif
    in a header file named persionType.h
    and the rest of my program in a .cpp file
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using namespace std;
    
    #include "personType.h"
    
    PersonType:personType()   	 //Default constructor;
    	{
      	    firstName; //store the first name    
    		lastName;  //store the last name
    	}
    		 void personType::print() 
    		 {
    	cout<<firstName<<" "<<lastName;
    		 }
    void personType::setName(string first, string last)
    {
    		firstName = first;
    		lastName = last;
    }
    
    return 0;
    }
    I get two errors that are:
    Code:
    .cpp(16) : error C2470: 'PersonType' : looks like a function definition, but there is no parameter list; skipping apparent body
    c.cpp(21) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    I can't figure out what is wrong here.
    If someone can help. It would be great.
    Thanks.
    Last edited by flicka; 10-22-2005 at 09:04 PM.

  3. #3
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Code:
    PersonType:personType()

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    thanks loko!
    I missed that one

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Ok! Corrected that but now I'm getting 2 new error messages that I don't understand. The error messages are:
    Code:
    .cpp(16) : error C2511: 'personType::personType(void)' : overloaded member function not found in 'personType'
            persontype.h(4) : see declaration of 'personType'
    .cpp(21) : error C2511: 'void personType::print(void)' : overloaded member function not found in 'personType'
            persontype.h(4) : see declaration of 'personType'
    If anyone has an idea on hwhat this means

  6. #6
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    you're defining the default constructor exactly how you should. But if you're using your own default constructor, you still need to declare its prototype in your class definition. so... pretty much you gotta add what's in the red:
    Code:
    class personType
    {
    public:
        void print() const;
        void setName(string first, string last);
        void getName(string& first, string& last);
        personType();
        personType(string first, string last);
    private:
       string firstName; //store the first name    
       string lastName;  //store the last name};
    };
    that should fix both your errors
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Thanks linucksrox !

    It solve the first one of my prolbems. Still Have the second one comming up
    Code:
    .cpp(22) : error C2511: 'void personType::print(void)' : overloaded member function not found in 'personType'
            persontype.h(4) : see declaration of 'personType'
    Would you have an idea?
    Thanks.

  8. #8
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    that one looks like where you define your
    Code:
    void personType::print()
    you have to say
    Code:
    void personType::print() const
    so that it matches exactly with your prototype, otherwise your compiler doesn't realize that you're defining THAT function. it thinks you're defining some OTHER function that just happens to "overload" the print() const function, it gets all confused, and then it stomps its feet and starts to whine...

    now before you do anything else, maybe it's just something that got messed up when you copied and pasted your .cpp file, but in the post where you show your code for the .cpp file, you've got your includes, then the function definitions, and then at the very end I see a
    Code:
    return 0;
    }
    which obviously will give you some errors. you might want to check that
    Last edited by linucksrox; 10-22-2005 at 10:58 PM.
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Quote Originally Posted by linucksrox
    so that it matches exactly with your prototype, otherwise your compiler doesn't realize that you're defining THAT function. it thinks you're defining some OTHER function that just happens to "overload" the print() const function, it gets all confused, and then it stomps its feet and starts to whine...
    That's a good one.

    Yes for the return it's a mistake from copying the .cpp

    I'll try that. Thanks.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    I made the changes and now I'm getting an error message that starts with this:

    Code:
    .cpp(23) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
            c:\program files\microsoft visual studio 8\vc\include\ostream(650): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
            with
            [
    Can anyone tell me what this means.
    Thanks.
    Last edited by flicka; 10-23-2005 at 08:54 AM.

  11. #11
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    well it looks to me like you've got a problem in your default constructor. i didn't catch that before, but that's not what i was looking for. you aren't initializing firstName or lastName. I don't know if that's the problem or not, but that's a start. and at the top of your program you don't need to say "using std::cout" and that stuff if you're just gonna say "using namespace std" because that covers everything. you just need "using namespace std" and while people might disagree with me and say you should do it explicitly, in your case the using namespace std should be good.
    maybe you should post your new .cpp file because it is obviously different than the original you already posted. that may help out a lot, especially so we can see what code you really have on line 23
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Thanks linucksrox !

    Here's the .cpp file that I have done.

    Code:
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    
    using std::string;
    using namespace std;
    
    #include "personType.h"
    
    personType::personType()   	 //Default constructor;
    	{
      	    firstName; //store the first name    
    		lastName;  //store the last name
    	}
    void personType::print() const 
    		 {
    	cout <<"\nThe name is: "<<firstName<<" "<<lastName<<endl;;
    		 }
    void personType::setName(string first, string last)
    {
    		firstName = first;
    		lastName = last;
    }
    It seems that it's coming from the print function.
    Last edited by flicka; 10-23-2005 at 03:18 PM.

  13. #13
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    I have reworked my program completely and I am now getting linking error when I use the compiler.
    Here is my header file:
    Code:
    #ifndef H_personType
    #define H_personType
    
    #include <string>
    
    using namespace std;
    
    class personType
    {
    public:
        void print() const;
        
        void setName(string first, string last);
        
        string getFirstName() const;
        
        string getLastName() const;
          
        personType(string first = "", string last = "");
           
     private:
        string firstName; //variable to store the first name
        string lastName;  //variable to store the last name
    };
    
    #endif
    Here my .cpp file:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    #include "personType.h"
    
    
    void personType::print() const
    {
    	cout << firstName << " " << lastName;
    }
    
    void personType::setName(string first, string last)
    {
    	firstName = first;
    	lastName = last;
    }
    
    string personType::getFirstName() const
    {
    	return firstName;
    }
    
    string personType::getLastName() const
    {
    	return lastName;
    }
    
    	//constructor
    personType::personType(string first, string last) 
    
    { 
    	firstName = first;
    	lastName = last;
    }
    Here are the error messages I get:

    Code:
    ------ Build started: Project: ATest1, Configuration: Debug Win32 ------
    Compiling...
    ATest1.cpp
    Linking...
    MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup
    Debug\ATest1.exe : fatal error LNK1120: 1 unresolved externals
    ATest1 - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Can anyone explain to me what this means and how I can fix this please.
    Thanks so much
    Last edited by flicka; 10-23-2005 at 06:06 PM.

  14. #14
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Not excatly sure but i dont see any main() anywhere there? is there a third file to this im assuming?
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  15. #15
    Registered User
    Join Date
    Jul 2005
    Posts
    45
    Well No third file for this one.

    I'm want first to get this one to print the first and last name and then I'll try to do one with or an adress book or a payroll type thing. It's more to understand the way to do it right because I see all these projects with headers and .cpp files.

    So should I add an int main ()??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM