classes and objects

This is a discussion on classes and objects within the C++ Programming forums, part of the General Programming Boards category; I have done this : Code: #include<iostream> #include<string> class student { private: int score; string surname; public: student(string asurname, int ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    44

    classes and objects

    I have done this :
    Code:
    #include<iostream>
    #include<string>
    
    class student
    {
    	private: 
    	
    	int score;
    	string surname;
    	
    	public:
    	
    	student(string asurname, int ascore);
    	~student();
    	string getSurname();
    	int getScore();
    };
    	student::student(string asurname, int ascore)
    	{
    		surname = asurname;
    		score = ascore;
    	}
    	
    	student::~student()
    	{
    	}
    	
    	string student::getSurname()
    	{
    		return surname;
    	}
    	
    	int student::getScore()
    	{
    		return score;
    	}
    
    
    using namespace std;
    
    
    int main()
    {
    	student Mike("Senikoglou", 20);
    	cout << Mike.getSurname();
    	cout << Mike.getScore();
    	
    	return 0;
    }
    having the class and the main fuction on the same cpp file, but how can I use 2 different files one for the class and one for the tester file(the main)???

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Put the class declaration in a header file, and the class definition in a .cpp file which #includes the header file.
    In the .cpp with the main(), #include the header file.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    Code:
    #include<iostream>
    #include<string>
    
    class student
    {
    	private: 
    	
    	int score;
    	string surname;
    	
    	public:
    	
    	student(string asurname, int ascore);
    	~student();
    	string getSurname();
    	int getScore();
    };
    Code:
    #include<string>
    #include<student.h>
    
    	student::student(string asurname, int ascore)
    	{
    		surname = asurname;
    		score = ascore;
    	}
    	
    	student::~student()
    	{
    	}
    	
    	string student::getSurname()
    	{
    		return surname;
    	}
    	
    	int student::getScore()
    	{
    		return score;
    	}
    Code:
    #include<iostream>
    #include<string>
    #include<student.h>
    
    using namespace std;
    /*student::student(string surname, int score);
    int student::getScore();
    string student::getSurname();*/
    
    int main()
    {
    	student Mike("Senikoglou", 20);
    	Mike.getSurname();
    	Mike.getScore();
    	
    	return 0;
    }
    I am having the following question. Do I have to declare the fuctions of my class in the main.cpp??? If yes how??? If I run with comments(it compiles) I get :


    [Error] C:\ undefined reference to `student::student(basic_string<char, string_char_traits<char>, __default_alloc_template<0, 0> >, int)'
    [Error] C:\: undefined reference to `student::~student(void)'
    [Error] C:\ undefined reference to `student::getSurname(void)'
    [Error] C:\: undefined reference to `student::getScore(void)'
    [Error] C:\: undefined reference to `student::~student(void)'

    --------------------without comments:-----------------

    [Error] C:\ declaration of `student::student(basic_string<char,string_char_tr aits<char>,__default_alloc_template<false,0> >, int)' outside of class is not definition
    [Error] C:: declaration of `int student::getScore()' outside of class is not definition
    [Error] C:\: declaration of `class string student::getSurname()' outside of class is not definition

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    No, you declare them in the header.
    I noticed you use string a lot in the header & .cpp files. Since you didn't put using namespace std; in the header (and you should NEVER EVER do that anyways) the compiler should be saying it doesn't know what string is.
    Change string to std::string in all places, since it's in the std namespace.

    BTW, what compiler are you using?
    If it's VC++, make sure all files are part of the Project.
    If you're using a makefile, make sure you're compiling both .cpp files and linking them both into the final executable.
    Last edited by cpjust; 05-05-2008 at 04:08 PM.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    I added using namespace in all of the 3 files. I still get ERROR 1 mentioned above

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Both cpp files have to be compiled and linked together. Are both added to your project/makefile/command line?

    Also, #include<student.h> should be #include "student.h". Use quotes for your own headers and brackets for library headers.

    Finally, don't forget to add header include guards to your header file. It probably doesn't matter here, but it might cause you a problem soon.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by mixalissen View Post
    I added using namespace in all of the 3 files.
    That's exactly what I said not to do.
    Read this: Chapter 59. Don't write namespace usings in a header file or before an #include

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    Well I found the solution. I need only 2 files instead of 3; thanx for the help; class deglaration and definition should be together...It seems that I misunderstood one of your suggestions...

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> class deglaration and definition should be together...

    No, that's wrong. It might work in this simple example, but that is not the "proper" way to divide the code up and will cause you problems in bigger projects.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    Code:
    #include<string>
    #include<student.h>
    class student
    {
    	public:
    	student(int asurname, int ascore)
    	{
    		surname = asurname;
    		score = ascore;
    	}
    	
    	student::~student()
    	{
    	}
    	
    	string student::getSurname()
    	{
    		return surname;
    	}
    	
    	int student::getScore()
    	{
    		return score;
    	}
    };
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    class student
    {
    	private: 
    	
    	int score;
    	int surname;
    	
    	public:
    	student(int surname, int score);
    	~student();
    	int getSurname();
    	int getScore();
    };
    Code:
    #include<iostream>
    #include<string>
    #include"student.h"
    
    using namespace std;
    
    int main()
    {
    	student Mike(23, 20);
    	cout << Mike.getSurname();
    	cout << Mike.getScore();
    	
    	return 0;
    }
    That's what I have so far. But I get " redefinition of class student" when I compile the first class. How is this solved???

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Go back to the code in post #3. That was correct after you made the std namespace change. The current code is not really close because you are defining the class twice.

    Then fix the project or makefile or command line to compile and link the files properly. As cpjust asked, what compiler/IDE are you using. That knowledge will help us help you solve the problem.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    C-free 4

    ps: still undefined reference

    what about linking??? fixing project ??? etc if you can pls be more specific I am new to this

    I fixed it, oh god it time to sleep.... Thanx a lot guys been helpful...
    Last edited by mixalissen; 05-05-2008 at 06:07 PM.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I'm not familiar with C-free 4, so I cannot give advice on how to use it properly. You have to find out how to tell it that you have two source files and that both need to be compiled and linked together. Are both the source files named with a cpp file extension?

    Is there a reason you are using that IDE instead of more common and free alternatives (like VC++ Express, Code::Blocks or Dev-C++)?

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    Actually I did not solve anything....F#$ck. All this time I have a file student.cpp that contained both declaration and definition of the class so main saw the fuctions from there and not fom the 2 latest
    files with seperate def and declar. I think that the answer will be in linking the files but I do not know how to do this. Anyway its getting late. I hope I get some answers tommorow...Thanx for your time guys...

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    44
    I was posting while you were posting... I have dev c++ yes both are cpp. How can I go about linking them with dev c++?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes & Objects [c++]
    By salmansalman in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2008, 08:02 AM
  2. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  3. static classes vs objects
    By earnshaw in forum C# Programming
    Replies: 5
    Last Post: 02-08-2006, 02:19 PM
  4. Accessing/working with member objects of parent classes
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2005, 10:01 PM
  5. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 01:23 PM

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