Like Tree1Likes
  • 1 Post By iMalc

c++ class seriously need a lot of help

This is a discussion on c++ class seriously need a lot of help within the C++ Programming forums, part of the General Programming Boards category; My main.cpp Code: #include <vector> #include "misc.h" #include "person.h" using namespace std; int main (void) { vector<Lecturer> vecLec; vector<Student> vecStd; ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    c++ class seriously need a lot of help

    My main.cpp
    Code:
    #include <vector>
    #include "misc.h"
    #include "person.h"
    
    using namespace std;
    
    
    
    int main (void) {
    
    	vector<Lecturer> vecLec;
    	vector<Student> vecStd;
    	insertData (vecLec, vecStd);
    
    	return 0;
    };
    My misc .cpp
    Code:
    //************************************
    //	misc.cpp
    //************************************
    
    #include "misc.h"
    
    
    string rmLeadSpc (string in) {
    	string temp = in;
    	size_t idx = temp.find_first_not_of (" \t\n\r");
    
    	if (idx != string::npos) {
    		temp = temp.substr (idx);
    	} else {
    		temp = string("");
    	};
    
    	return temp;
    };
    
    
    string rmTrailSpc (string in) {
    	string temp = in;
    	size_t idx = temp.find_last_not_of (" \t\n\r");
    
    	if (idx != string::npos) {
    		temp = temp.substr (0, idx + 1);
    	};
    
    	return temp;
    };
    
    
    vector<string> tokenise (string in, string delim) {
    
    	// delimeter cannot be empty
    	if (delim.empty()) delim = ",";
    
    	vector<string> tokens;
    	string::size_type i = 0, j = 0;
    
    	while (i != string::npos) {
    		j = in.find_first_of (delim, i);
    		string token = rmLeadSpc(rmTrailSpc(in.substr (i, j - i)));
    		if (!token.empty()) tokens.push_back (token);
    
    		if (j == string::npos) break;
    		i = j + 1;
    	}
    
    	return tokens;
    };
    misc.h
    Code:
    //************************************
    //	misc.h
    //************************************
    
    #ifndef MISC_H
    #define MISC_H
    
    #include <string>
    #include <vector>
    
    
    using namespace std;
    
    
    enum PersonType {
    	LECTURER,
    	STUDENT
    };
    
    
    class Date {
    	private:
    		int year;
    		int month;
    		int day;
    
    	public:
    		void setYear	(int inYear)	{year = inYear;};
    		void setMonth	(int inMonth)	{month = inMonth;};
    		void setDay		(int inDay)		{day = inDay;};
    
    		int getYear		(void)	{return year;};
    		int getMonth	(void)	{return month;};
    		int getDay		(void)	{return day;};
    };
    
    
    string rmLeadSpc (string in);
    string rmTrailSpc (string in);
    vector<string> tokenise (string in, string delim = ",");
    
    #endif
    My Person.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "Person.h"
    #include "misc.h"
    
    using namespace std;
    
    Person::Person(PersonType inPType, string inName, string inNric)
    {
    	type = inPType;
    	name = inName;
    	nric = inNric;
    }
    
    void Person:: setDob (int inYear, int inMonth, int inDay)
    {
    	dob.setYear(inYear);
    	dob.setMonth(inMonth);
    	dob.setDay(inDay);
    
    }
    
    PersonType Person::getType (void)
    {
    	//PersonType.Lecturer;
    	//PersonTypw.Student;
    }
    
    string Person::getName(void)
    {
    	return name;
    }
    
    string Person::getNric(void)
    {
    	return nric;
    }
    
    int Person::getDobYear(void)
    {
    	return inYear;
    }
    
    
    
    
    
    
    int Person::getDobMonth(void)
    {
    	return inMonth;
    }
    
    int Person::getDobDay(void)
    {
    	return inDay;
    }
    
    
    
    
    Lecturer::Lecturer(string inName, string inNric, string inRoom, int inSal)
    :Person(PersonType inPType, string inName, string inNric)
    {
    	room = inRoom;
    	salary = inSal;
    }
    
    void Lecturer::increment(int pct)
    {
    	pct = salary * 0.1 + salary;
    }
    
    int Lecturer::getCount (void)
    {
    }
    
    string Lecturer::getRoom(void)
    {
    	return room;
    }
    
    int Lecturer::getSalary(void)
    {
    	return salary;
    }
    
    
    
    
    
    Student::Student(string inName, string inNric, string inAdminNo, string inDiploma)
    :Person(PersonType inPType, string inName, string inNric)
    {
    	
    	adminNo = inAdminNo;
    	diploma = inDiploma;
    }
    
    int Student::avgScores(void)
    {
    }
    Person.h
    Code:
    //************************************
    //	person.h
    //************************************
    
    #ifndef PERSON_H
    #define PERSON_H
    
    
    #include <string>
    #include <vector>
    #include "misc.h"
    
    using namespace std;
    
    
    class Person {
    	private:
    		PersonType type;
    		string name;
    		string nric;
    		Date dob;
    
    	public:
    		Person (PersonType inPType, string inName, string inNric);
    		void setDob (int inYear, int inMonth, int inDay);
    
    		PersonType getType (void);
    		string	getName	(void);
    		string	getNric	(void);
    		int		getDobYear	(void);
    		int		getDobMonth	(void);
    		int		getDobDay	(void);
    };
    
    
    class Lecturer : public Person {
    	private:
    		string room;
    		int salary;
    		static int count;
    
    	public:
    		Lecturer (string inName, string inNric, string inRoom, int inSal);
    		void increment (int pct);
    
    		static int getCount (void);
    		string getRoom (void);
    		int getSalary (void);
    };
    
    
    class Student : public Person {
    	private:
    		string adminNo;
    		string diploma;
    		static int count;
    
    		int scores[3];
    		string grades[3];
    
    	public:
    		Student (string inName, string inNric, string inAdminNo, string inDiploma);
    
    		static int getCount (void) {return count;};
    		int getScores (int module) {return scores[module];};
    		string getGrades (int module) {return grades[module];};
    		string getAdminNo (void) {return adminNo;};
    		string getDiploma (void) {return diploma;};
    
    		int avgScores (void);
    
    };
    
    
    
    
    
    void insertLec (vector<Lecturer> & vecLec, vector<string> parts);
    int avgSal (vector<Lecturer> & vecLec);
    
    void insertStd (vector<Student> & vecStd, vector<string> parts);
    int avgScore (vector<Student> & vecStd, int module);
    int getCount (vector<Student> & vecStd, string diploma);
    
    void insertData (vector<Lecturer> & vecLec, vector<Student> & vecStd);
    
    #endif
    Seriously, this is the most difficult part in my college lesson and the assignment that I had to be done. All my basic Person.cpp is the part where I think I had wrongly declared, especially the part where the enum with STUDENT and LECTURER and the function getType with PersonTYpe declared instead of void or int. How do I write in Person.cpp code, i completely lost this part here. Another part is the part where I had done using composition. I think the Person.cpp for the code is completely wrong for the composition. Give me some advice please. Anyway, alll the other codes are written by my lecturer expect the Person.cpp which is wrote by me. So, all the files have no problem expect the Person.cpp
    Last edited by evildotaing; 12-05-2011 at 09:24 AM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    In the constructor for Lecturer, drop the type specifiers from Person(), as if you were calling it like a function:
    Code:
    Lecturer::Lecturer(string inName, string inNric, string inRoom, int inSal)
    :Person(inPType, inName, inNric)
    {
        ...
    }
    Consider this post signed

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Settle down, we can't help someone who is essentially panicing.
    We need to approach this one thing at a time. First and formost we need to get the code to compile. Currently it will not compile for the reason the above poster specified. There may be more things stopping it too.
    So your first step in allowing us to help you is for you to post the list of compile errors.
    Next, pick a function that isn't completed or isn't doing what it is supposed to and tell us what the problem with it is.

    By the way, if person.h was supplied to you then I can tell you that the code you are being given is of poor quality. It uses redundant keywords, utterly lacks const-correctness, and is very inefficient. It turns out that it is very hard to find a C++ course given by someone competent enough in the language themselves to properly teach it to others.
    Last edited by iMalc; 12-05-2011 at 11:59 PM.
    dennis.cpp likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    No choice, lecturer said we must use its code and we are to add to make all the class work. Seriously due to this, i find it extremely hard even for just taking out the error , not really for the program to work.
    My error.
    1>------ Build started: Project: c++ Assignment 1, Configuration: Debug Win32 ------
    1>Compiling...
    1>Person.cpp
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(41) : error C2065: 'inYear' : undeclared identifier
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(47) : error C2065: 'inMonth' : undeclared identifier
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(52) : error C2065: 'inDay' : undeclared identifier
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(59) : error C2275: 'PersonType' : illegal use of this type as an expression
    1> e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\misc.h(15) : see declaration of 'PersonType'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(59) : error C2146: syntax error : missing ')' before identifier 'inPType'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(59) : error C2612: trailing 'identifier' illegal in base/member initializer list
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(59) : error C2065: 'inPType' : undeclared identifier
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(59) : error C2275: 'std::string' : illegal use of this type as an expression
    1> c:\program files\microsoft visual studio 9.0\vc\include\xstring(2210) : see declaration of 'std::string'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(59) : error C2146: syntax error : missing ';' before identifier 'inName'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(59) : error C2275: 'std::string' : illegal use of this type as an expression
    1> c:\program files\microsoft visual studio 9.0\vc\include\xstring(2210) : see declaration of 'std::string'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(59) : error C2146: syntax error : missing ';' before identifier 'inNric'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(59) : error C2059: syntax error : ')'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(60) : error C2143: syntax error : missing ';' before '{'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(66) : error C2601: 'Lecturer::increment' : local function definitions are illegal
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(71) : error C2601: 'Lecturer::getCount' : local function definitions are illegal
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(75) : error C2601: 'Lecturer::getRoom' : local function definitions are illegal
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(80) : error C2601: 'Lecturer::getSalary' : local function definitions are illegal
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(88) : error C2275: 'std::string' : illegal use of this type as an expression
    1> c:\program files\microsoft visual studio 9.0\vc\include\xstring(2210) : see declaration of 'std::string'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(88) : error C2146: syntax error : missing ')' before identifier 'inName'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(88) : error C2059: syntax error : ')'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(90) : error C2143: syntax error : missing ';' before '{'
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(92) : error C2065: 'adminNo' : undeclared identifier
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(92) : error C2065: 'inAdminNo' : undeclared identifier
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(93) : error C2065: 'diploma' : undeclared identifier
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(93) : error C2065: 'inDiploma' : undeclared identifier
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(97) : error C2601: 'Student::avgScores' : local function definitions are illegal
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(103) : fatal error C1004: unexpected end-of-file found
    1>Build log was saved at "file://e:\DM2121 Advanced C++ Programming\c++ Assignment 1\c++ Assignment 1\Debug\BuildLog.htm"
    I would tried to solve some of it but some of the error like those composition constructor is extremely difficult. Need some advice.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    In line 62-63 of the Person.cpp constructor, how should I code it, please help me

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Somebody told you nineteen hours ago. You only need to read, understand and copy.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    >>Lecturer::Lecturer(string inName, string inNric, string inRoom, int inSal)
    >>:Person(PersonType inPType, string inName, string inNric)
    The initializer list can contain function calls, but it is not a declaration. What you have is a declaration, which is wrong. You need to call that function (or constructor), not declare it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Ok solved, my code.
    Code:
    Lecturer::Lecturer(string inName, string inNric, string inRoom, int inSal)
    	:Person(LECTURER, inName, inNric)
    But I not sure how to write the vector functions in line 78 onwards. Any1 know how to write the function. This is my function vector but completely wrong.
    Code:
    void insertLec (vector<Lecturer> & vecLec, vector<string> parts);

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Person.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "Person.h"
    #include "misc.h"
    
    using namespace std;
    
    Person::Person(PersonType inPType, string inName, string inNric)
    {
    	type = inPType;
    	name = inName;
    	nric = inNric;
    }
    
    void Person:: setDob (int inYear, int inMonth, int inDay)
    {
    	dob.setYear(inYear);
    	dob.setMonth(inMonth);
    	dob.setDay(inDay);
    
    }
    
    PersonType Person::getType (void)
    {
    	if (type == STUDENT)
    		return STUDENT;
    	else
    		return LECTURER;
    }
    
    
    string Person::getName(void)
    {
    	return name;
    }
    
    string Person::getNric(void)
    {
    	return nric;
    }
    
    int Person::getDobYear(void)
    {
    	
    	return dob.getYear();
    }
    
    
    int Person::getDobMonth(void)
    {
    	return dob.getMonth();
    }
    
    int Person::getDobDay(void)
    {
    	return dob.getDay();
    }
    
    int Lecturer::count = 0;
    
    Lecturer::Lecturer(string inName, string inNric, string inRoom, int inSal)
    	:Person(LECTURER, inName, inNric)
    {
    	
    	room = inRoom;
    	salary = inSal;
    }
    
    
    void Lecturer::increment(int pct)
    {
    	pct = salary * 1 + salary;
    }
    
    int Lecturer::getCount (void)
    {
    	return count;
    }
    
    string Lecturer::getRoom(void)
    {
    	return room;
    }
    
    int Lecturer::getSalary(void)
    {
    	return salary;
    }
    
    
    
    int Student::count = 0;
    
    Student::Student(string inName, string inNric, string inAdminNo, string inDiploma)
    :Person( STUDENT, inName,  inNric)
    {
    	Person::Person( STUDENT,  inName,  inNric);
    	adminNo = inAdminNo;
    	diploma = inDiploma;
    }
    
    int Student::avgScores(void)
    {
    	for (int i = 0; i <= 3; i++)
    	{
    		return scores[i] /3;
    	}
    }
    /*
     void insertLec::(vector<Lecturer> & vecLec, vector<string> parts)
    {
    	
    }
    */
    /*
    int avgSal::(vector<Lecturer> & vecLec)
    {
    }
    
    void insertStd:: (vector<Student> & vecStd, vector<string> parts)
    {
    }
    int avgScore:: (vector<Student> & vecStd, int module)
    {
    }
    int getCount:: (vector<Student> & vecStd, string diploma)
    {
    }
    
    void insertData:: (vector<Lecturer> & vecLec, vector<Student> & vecStd)
    {
    }
    */

    1>------ Build started: Project: c++ Assignment 1, Configuration: Debug Win32 ------
    1>Compiling...
    1>Person.cpp
    1>e:\dm2121 advanced c++ programming\c++ assignment 1\c++ assignment 1\person.cpp(109) : warning C4715: 'Student::avgScores' : not all control paths return a value
    1>Linking...
    1>main.obj : error LNK2019: unresolved external symbol "void __cdecl insertData(class std::vector<class Lecturer,class std::allocator<class Lecturer> > &,class std::vector<class Student,class std::allocator<class Student> > &)" (?insertData@@YAXAAV?$vector@VLecturer@@V?$allocat or@VLecturer@@@std@@@std@@AAV?$vector@VStudent@@V? $allocator@VStudent@@@std@@@2@@Z) referenced in function _main
    1>E:\DM2121 Advanced C++ Programming\c++ Assignment 1\Debug\c++ Assignment 1.exe : fatal error LNK1120: 1 unresolved externals
    1>Build log was saved at "file://e:\DM2121 Advanced C++ Programming\c++ Assignment 1\c++ Assignment 1\Debug\BuildLog.htm"
    1>c++ Assignment 1 - 2 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Any1 could figure what is the error that I received.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    avgScores is way wrong.
    The linker error is because you haven't implemented (or actually created) a function named insertData (which is called from main).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2008, 03:31 AM
  2. Replies: 7
    Last Post: 05-26-2005, 11:48 AM
  3. Replies: 2
    Last Post: 04-06-2005, 08:25 AM
  4. Template <class T1, class T2, class T3> error LNK2019
    By JonAntoine in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2004, 01:25 PM
  5. Replies: 1
    Last Post: 12-11-2002, 10:31 PM

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