9 Replies - 546 Views - Last Post: 03 April 2009 - 09:50 AM Rate Topic: -----

#1 miickee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 09

Errors in display function

Posted 02 April 2009 - 08:47 PM

Hey there

I have a display function in my
ResearchStudent.h file:
#include "Student.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#ifndef RESEARCH_STUDENT
#define RESEARCH_STUDENT

class ResearchStudent : public Student
{
	
private:
	string myDegree;
	string currentMilestone;

public:
	ResearchStudent(long id, string last, string first, string category, string degree, string milestone);
	void displayInfo(ostream & out) const;
	void get();
	void set();
	void displayAssessment();
	void calcMilestone(string milestone);


};

#endif 



ResearchStudent.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include "ResearchStudent.h"

using namespace std;

//#ifndef RESEARCH_STUDENT
//#define RESEARCH_STUDENT

ResearchStudent::ResearchStudent(long id, string last, string first, string category, string degree, string milestone)
: Student(id, last, first, category), myDegree(degree), currentMilestone(milestone)
{
	myDegree = degree;
	currentMilestone = milestone;
}

string newMilestone;
string milestone;


string calcMilestone(string milestone)
{
	if (milestone=="new-enrolled") {
		newMilestone = "research-proposal-submission";
	} else if (milestone=="topic-confirmed") {
		newMilestone = "confirmation-report-submission";
	} else if (milestone=="candidature-confirmed") {
		newMilestone = "thesis-submission";
	} else if (milestone=="degree-completion") {
		newMilestone = "finished";
	};
	return newMilestone;
}

void ResearchStudent::get()
{}

void ResearchStudent::set()
{}

void ResearchStudent::displayInfo(ostream & out) const
{
	Student::displayInfo(cout);
	out << "Degree: " << myDegree << endl;
	out << "Milestone: " << currentMilestone << endl;
	out << "Next Milestone: " << calcMilestone(currentMilestone);
	out << endl;

}

void ResearchStudent::displayAssessment()
{}

//

//#endif


Ok, my calcMilestone(currentMilestone) gets this error:

Quote

1>------ Build started: Project: StudentAssessment2, Configuration: Debug Win32 ------
1>Compiling...
1>ResearchStudent.cpp
1>c:\users\miickee\documents\visual studio 2005\projects\studentassessment2\studentassessment2\researchstudent.cpp(47) : error C2662: 'ResearchStudent::calcMilestone' : cannot convert 'this' pointer from 'const ResearchStudent' to 'ResearchStudent &'
1> Conversion loses qualifiers
1>Generating Code...
1>Compiling...
1>ClientProgram.cpp
1>Generating Code...


now, I have been told that it is because one is const and the other is not (displayInfo and calcMilestone). However, making them both not const and both const does not work either, cause it gives me errors.

Any thoughts?

Michael

Is This A Good Question/Topic? 0
  • +

Replies To: Errors in display function

#2 Darkhack  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 208
  • Joined: 25-November 08

Re: Errors in display function

Posted 02 April 2009 - 09:09 PM

You have calcMilestone as a member of your ResearchStudent class in the header file, but you don't have the scope resolution operator in the source file like you do with your other functions.

newMilestone and milestone can't be declared randomly in the middle of the source file like that. Declare them in the scope that you are using them. I would also recommend a switch function instead of all the if-else statements, plus you don't even need the newMilestone variable inside of calcMilestone(). You're just returning it right away anyway. Your function should end up looking like this.

string ResearchStudent::calcMilestone(string milestone)
{
	// don't need "break;" since return terminates function
	switch(milestone)
	{
		case "new-enrolled": return "research-proposal-submission";
		case "topic-confirmed": return "confirmation-report-submission";
		case "candidature-confirmed": return "thesis-submission";
		case "degree-completion": return "finished";
		default: return "error";  // just in case
	}
}


This post has been edited by Darkhack: 02 April 2009 - 09:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 miickee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 09

Re: Errors in display function

Posted 03 April 2009 - 04:58 AM

View PostDarkhack, on 2 Apr, 2009 - 08:09 PM, said:

You have calcMilestone as a member of your ResearchStudent class in the header file, but you don't have the scope resolution operator in the source file like you do with your other functions.


Thanks for your reply. However, this bit above in quotes I have trouble understanding. Could I have an explanation in layman's terms?

miickee

EDIT***

I think what you meant with the scope resolution operator:
out << "Next Milestone: " << ResearchStudent::calcMilestone(currentMilestone);


I did this, and it gives the error:
1>c:\users\miickee\documents\visual studio 2005\projects\studentassessment2\studentassessment2\researchstudent.cpp(54) : error C2662: 'ResearchStudent::calcMilestone' : cannot convert 'this' pointer from 'const ResearchStudent' to 'ResearchStudent &'


This post has been edited by miickee: 03 April 2009 - 05:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Errors in display function

Posted 03 April 2009 - 05:11 AM

There's nothing that ties your calcMilestone function to the ResearchStudent class.

See this:
string calcMilestone(string milestone)
{
   // ...
}


is just a function definition, and is not part of your class. You need to do this:
string ResearchStudent::calcMilestone(string milestone)
{
   // ...
}


to make it part of your class. The ResearchStudent:: part of that is the "scope resolution operator", although it's not really an operator in this case, but it performs the same role...it tells the compiler that this function is the implementation of the calcMilestone method declared in the ResearchStudent class.
Was This Post Helpful? 0
  • +
  • -

#5 miickee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 09

Re: Errors in display function

Posted 03 April 2009 - 06:27 AM

Thanks JackOfAllTrades, that makes a lot more sense now.

I think this is a better way of doing it, calling the calcMilestone function first, then outputting the newMilestone variable once it has finished.

ResearchStudent::calcMilestone(currentMilestone);
	out << "Next Milestone: " << newMilestone;


But it gives me LNK2001 and LNK1120 errors:
1>ResearchStudent.obj : error LNK2001: unresolved external symbol "public: void __thiscall ResearchStudent::calcMilestone(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)const " (?calcMilestone@ResearchStudent@@QBEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>C:\Users\miickee\Documents\Visual Studio 2005\Projects\StudentAssessment2\Debug\StudentAssessment2.exe : fatal error LNK1120: 1 unresolved externals


What does this mean?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Errors in display function

Posted 03 April 2009 - 07:25 AM

We weren't talking about calling the function, we're talking about DEFINING the function. You still haven't DEFINED the function properly!
string ResearchStudent::calcMilestone(string milestone)
{
    // Function definition
}


Compare your current calcMilestone function definition to the other function definitions!
Was This Post Helpful? 0
  • +
  • -

#7 miickee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 09

Re: Errors in display function

Posted 03 April 2009 - 08:25 AM

Thank you that helped me, it works finally! yay

Now the last part, it is giving me the sh**s.

CourseWorkStudent.h:
#include "Student.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

#ifndef COURSEWORK_STUDENT
#define COURSEWORK_STUDENT

class CourseWorkStudent : public Student
{

private:
	int GPA;
	int noOfUnits;
	int classGradeArray[10];
	string classUnitArray[10];

public:
	CourseWorkStudent(long id, string last, string first, string category, int numUnits, string unitArray[], int gradeArray[]);

	void displayInfo(ostream & out) const;
	void get();
	void set();
	void displayAssessment();
	int calcGPA(int gradeArray[], int numUnits) const;

};


#endif 




CourseWorkStudent.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "CourseWorkStudent.h"

using namespace std;

//#ifndef COURSEWORK_STUDENT
//#define COURSEWORK_STUDENT


CourseWorkStudent::CourseWorkStudent(long id, string last, string first, string category, int numUnits, string unitArray[], int gradeArray[])
: Student(id, last, first, category), noOfUnits(numUnits)
{
	for (int i=0; i < (2 * numUnits); i++)
	{
		classUnitArray[i] = unitArray[i];
	}

	for (int i=0; i < numUnits; i++)
	{
		classGradeArray[i] = gradeArray[i];
	}
}


void CourseWorkStudent::displayInfo(ostream & out) const
{
	Student::displayInfo(cout);
	out << "Grades: ";
	for (int i=0; i<noOfUnits; i++)
	{
		if (i==noOfUnits-1)
		{
			out << classUnitArray[i] << " " << classGradeArray[i] << " ";
		}
		else {
			out << classUnitArray[i] << " " << classGradeArray[i] << ", ";
		}
	}
	out << endl;
	out << endl;
	out << "GPA : " << CourseWorkStudent::calcGPA(classGradeArray, noOfUnits);
}


void CourseWorkStudent::set()
{}

void CourseWorkStudent::get()
{}

void CourseWorkStudent::displayAssessment()
{}


int CourseWorkStudent::calcGPA(int gradeArray[], int numUnits) const
{
	int GPA;
	int i;
	int sum = 0;
	for(i=0;i<numUnits;i++)
	{
					
		sum += gradeArray[i];
		i++;
		GPA = sum / numUnits;
	};
	return GPA;
};
		


//#endif



Gives me the following error:
1>c:\users\miickee\documents\visual studio 2005\projects\studentassessment2\studentassessment2\courseworkstudent.cpp(44) : error C2664: 'CourseWorkStudent::calcGPA' : cannot convert parameter 1 from 'const int [10]' to 'int []'



Ok ok the prototype in CourseWorkStudent.h if you hover over the gradeArray[] parameter in the calcGPA prototype, it says "int gradeArray", and if you look at the declaration in CourseWorkStudent.h and hover over the argument I put into the function, it comes up with "in CourseWorkStudent::classGradeArray[10]"...
I would like them to be the same :S

This post has been edited by miickee: 03 April 2009 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Errors in display function

Posted 03 April 2009 - 08:56 AM

You don't show where you're calling calcGPA, which is essential for this error.
Was This Post Helpful? 0
  • +
  • -

#9 miickee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 09

Re: Errors in display function

Posted 03 April 2009 - 09:27 AM

I'm calling it in the CourseWorkStudent.cpp file:
out << "GPA : " << CourseWorkStudent::calcGPA(classGradeArray, noOfUnits);

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: Errors in display function

Posted 03 April 2009 - 09:50 AM

Ah OK, sorry...it was scrolled off the screen so I missed it. I'm not sure why, from the code, you're getting this error.

You actually do not need to pass the array argument to calcGPA, because calcGPA is a member function and therefore has access to the classGradeArray member. You could make that function take no arguments at all, and just use the classGradeArray and noOfUnits members.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1