Problem with character arrays in classes

This is a discussion on Problem with character arrays in classes within the C++ Programming forums, part of the General Programming Boards category; I have this project to do and I'm having serious isuses with it. I have a character array as a ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    Problem with character arrays in classes

    I have this project to do and I'm having serious isuses with it.

    I have a character array as a private object of a class, Im trying to use public accessors to get it in the main program, but when I do, it tells me Im trying to convert char[50] to char

    When I make just the strings public i get the error : driver.obj : error LNK2001: unresolved external symbol "public: void __thiscall Course::SetRoom(unsigned short)" (?SetRoom@Course@@QAEXG@Z)

    When everything is public it all runs fine. Problem is they all the objects need to run private.

    Any help would be veryyyyyy appreciated. I'll post the coding to the three files below. thanks.

    classes.h file
    Code:
    #include <iostream>
    using namespace std;
    
    class Course
    {
    	public:
    		//public accessors
    		unsigned short GetNumber();
    		void SetNumber(unsigned short number);
    
    		unsigned short GetCredHours();
    		void SetCredHours(unsigned short creditHours);
    		
    		unsigned short GetContHours();
    		void SetContHours(unsigned short contactHours);
    
    		unsigned short GetTime();
    		void SetTime(unsigned short time);
    
    		unsigned short GetRoom();
    		void SetRoom(unsigned short room);
    
    		char GetDescription();
    		void SetDescription(char description[50]);
    	
    		char GetInstructor();
    		void SetInstructor(char instructor[50]);		
    		
    		//public function
    		void CourseDatabaseBuild();
    
    	private:
    		unsigned short number, creditHours, contactHours, time, room;
    		char description[50], instructor[50];
    };
    
    class Student
    {
    	public:
    		//public accessors
    		unsigned short GetID();
    		void SetID(unsigned short student_id);
    
    		char GetName();
    		void SetName(char student_name[50]);
    
    	private:
    		unsigned short student_id;
    		char student_name[50];
    };
    driver.cpp file
    Code:
    #include <iostream>
    #include "classes.h"
    using namespace std;
    
    Course Coursedb[20];
    Student Studentdb[20];
    unsigned short i=0, j=0, h=0, k=0,m=0, n=0, p=1;
    unsigned short schedule[5], found=0, curStud=0, curCourse=0, number, credHours, time, contHours;
    unsigned short schedTotal=0, courseNum, courseTotal, studentTotal, rmNum, ident;
    char choice;
    char title[50];
    char instruc[50];
    char name[50];
    char description[50];
    
    
    void BuildCourseDB()
    {
    	//The function used to build the course database
    
    	//Get first course from user
    	cout<<"Enter course number: ";
    	cin>>number;
    	Coursedb[j].SetNumber(number);
    	
    	
    	//Loop to get all course information until the user types in 999
    	while(number != 999 && j <= 20)
    	{
    	    cin.ignore();
    		cout<<"Enter course title: ";
    		cin.getline(title, 49);
    		Coursedb[j].SetDescription(title);
    
    		cout<<"Enter course credit hours: ";
    		cin>>credHours;
    		Coursedb[j].SetCredHours(credHours);
    
    		cout<<"Enter course meeting time hour (Number entered must be an integer 1-24)";
    		cin>>time;
    		Coursedb[j].SetTime(time);
    
    		cout<<"Enter total contact hours per week for this course: ";
    		cin>>contHours;
    		Coursedb[j].SetContHours(contHours);
    
    		cout<<"Enter room number this course is being held in: ";
    		cin>>rmNum;
    		Coursedb[j].SetRoom(rmNum);
    		
    		cin.ignore();
            cout<<"Enter instructor for this course: ";
    		cin.getline(instruc, 49);
    		Coursedb[j].SetInstructor(instruc);
    		
    		j++;
    
    		//Get second course or loop terminating 999 sentinel
    		cout<<"Enter course number: ";
    		cin>>number;
    		Coursedb[j].SetNumber(number);
    	}
    	courseTotal = j;
    }
    
    void BuildStudentDB()
    {
        //Function to build the student database
    
    	//Get first student information from user
    	cout<<"Please enter the first Student ID Number: ";
    	cin>>ident;
    	Studentdb[i].SetID(ident);
    	
    	//Loop to get all student information until 999 is entered
    	while(ident != 999 && i <= 20)
    	{
    	    cin.ignore();
    	    cout<<"Please enter the name of student "<<ident<<":  ";
    		cin.getline(name, 49);
    		Studentdb[i].SetName(name);
    
    		i++;
    		
    		//Next student
    		cout<<"Please enter the next Student ID Number: ";
    		cin>>ident;
    		Studentdb[i].SetID(ident);
    	}
    	studentTotal = i;
    }
    
    void CreateSchedule()
    {
        //Function which uses the student database and course database to compile and
        //print out a schedule for each student.
    	choice = 'y';
    	while((choice == 'y' || choice == 'Y') && curStud <= studentTotal)
    	{
    		name = Studentdb[curStud].GetName();
    		cout<<"Please enter the first course you would like to register "
    			<<name<<" for: ";
    		cin>>courseNum;
    
    		while(courseNum != 999)
    		{
    			while(h<=courseTotal && found == 0)
    			{
    				number = Coursedb[h].GetNumber();
    				if(courseNum == number)
    				{
    					found = 1;
    					schedule[k]=courseNum;
    					k++;
    					description = Coursedb[h].GetDescription();
    					cout<<"Confirmed registration for "<<description<<"."<<endl;
    				}
    				else
    				{
    					found = 0;
    					h++;
    				}
    
    			}
    			h=0;
    			name = Studentdb[curStud].GetName();
    			cout<<"Please enter the first course you would like to register "
    				<<name<<" for: ";
    			cin>>courseNum;
    			
    			schedTotal++;
    			found = 0;
    		}
    		cout<<endl<<endl<<endl;
    		name = Studentdb[curStud].GetName();
    		ident = Studentdb[curStud].GetID();
    		cout<<"Name: "<<name<<"\tID: "<<ident;
    		cout<<endl<<endl;
    		k=0;
    		n=0;
    		while(k <= schedTotal)
    		{
    			while(h<=courseTotal)
    			{
    				number = Coursedb[h].GetNumber();
    				if(schedule[n] == number)
    				{
    					title = Coursedb[h].GetDescription();
    					instruc = Coursedb[h].GetInstructor();
    					time = Coursedb[h].GetTime();
    					credHours = Coursedb[h].GetCredHours();
    					contHours = Coursedb[h].GetContHours();
    					rmNum = Coursedb[h].GetRoom();
    					cout<<number<<"\t"<<title<<"\t"<<instruc<<endl;
    					cout<<"\tTime: "<<time<<":00\tCredits: "<<credHours<<endl;
    					cout<<"\tContact Hours: "<<contHours<<"\tRoom: "<<rmNum<<endl;
    					n++;
    					h=0;
    				}
    				else
    					h++;
    			}
    			k++;
    		}
    		n=0;
    		h=0;
    		p++;
    		if(p<=studentTotal)
    		{
    			cout<<"Would you like to enter information for the next student?  (y/n)";
    			cin>>choice;
    			if(choice=='y' || choice=='Y')
    				curStud++;
    		}
    		else
    			choice='n';
    	}	
    }
    main.cpp

    Code:
    #include <iostream>
    #include "classes.h"
    using namespace std;
    
    int main()
    {
    	void BuildCourseDB();
    	void BuildStudentDB();
    	void CreateSchedule();
    	
    	//Program intro
    	cout<<endl<<"Welcome To The University Of Yourstate Registration System" <<endl
    		<<"This system will allow you to first build a course database and then register"<<endl
    		<<"multiple students into it printing out schedules for each student as you finish.";
    	cout<<endl;
    	cout<<endl<<endl;
    	
    	//Course intro
    	cout<<"To start, let's build the course database."<<endl
    		<<"\tEnter all requested data for each course, when all courses are entered."<<endl
    		<<"\tEnter 999 for the following course number and the program will advance."
    		<<endl<<endl;
    
    	//Function to build course database
    	BuildCourseDB();
    
    	//Student intro
    	cout<<endl<<"Great, now that we have our courses done, let's start on the student"<<endl
    		<<"registration."<<endl
    		<<"\tOnce again enter all requested data for each student. Use the ID 999 "<<endl
    		<<"\tto terminate the program."<<endl<<endl;
    
    	//Function to build student database
    	BuildStudentDB();
    	
    	cout<<endl<<endl;
    	
    	
    	//Run function to create the schedule for each student and print it to screen	
       CreateSchedule();
    
    
    	//Program termination
    	cout<<endl<<endl<<"Thank you for using our software, have a great day!"<<endl<<endl;
    	return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Where is classes.cpp?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    2
    classes are in the header file classes.h which is the first coding i posted.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    But
    void SetRoom(unsigned short room);
    isn't enough.


    Somewhere along the line, you need to actually write some code, not just state it exists

    Code:
    void course::SetRoom ( unsigned short room ) {
      // do stuff
    }
    Every .h file containing a class typically has a .cpp file containing the implementation of that class.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Character problem!!
    By cBegginer in forum C Programming
    Replies: 3
    Last Post: 09-02-2005, 11:51 PM
  4. Arrays with base/derived classes
    By shaeng in forum C++ Programming
    Replies: 3
    Last Post: 05-31-2004, 11:54 AM
  5. Problem with arrays and pointers
    By Dupek in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2002, 11:23 AM

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