Having some trouble with converting my driver input from one class to the friend class and then summing the array. My setGrade function is reading the input correctly (I think, the numbers match up) but I need to pass the class to the friend in order to add up the values. Herein lies the aggravation. The errors I've been getting are that Grade is an undeclared identifier, or that I can't make the conversion from the setGrade variable to something I can work with in the other class. I'll also need to use the '==' operator eventually but I'd rather get to that after I deal with the previous mess. That said however, I'd appreciate any advice anyone could give me. Thanks in advance. Here's the code:

[LIST=1]
[*]#include<iostream>

[*]using namespace std;
[*]const int MAX_GRADES = 4;

[*]class Student;

[*]class Student
[*]{
[*]	private:
[*]		int Id;
[*]		float FinalGrade;
[*]		float Grade[MAX_GRADES];

[*]	public:
[*]		void setId(int i){Id = i;};
[*]		void setGrade(float G[])
[*]		{
[*]			for (int i=0;i<MAX_GRADES;i++)
[*]				cout << G[i]<< endl;
[*]		};
[*]	//	bool operator == (Student x,y){if x.FinalGrade == y.FinalGrade return true; else return false;};
[*]		friend class Teacher;

[*]};
[*]class Teacher
[*]{
[*]private:
[*]	int Id;
[*]	float Salary;
[*]public:
[*]	float calculateGrade(Student G[])
[*]	{
[*]		float FinalGrade = 0;
[*]		Grade[0] = G[0];
[*]		Grade[1] = G[1];
[*]		Grade[2] = G[2];
[*]		Grade[3] = G[3];
[*]		
[*]		FinalGrade = Grade[0] + Grade[1] + Grade[2] + Grade[3]; 
[*]		return FinalGrade;
[*]	};
[*]	
[*]	
[*]};

[*]int main()
[*]{
[*]float g1[] = {10.0, 20.0, 25.0, 25.0};
[*]float g2[] = {25.0, 20.0, 10.0, 25.0};
[*]Student x, y;
[*]x.setGrade(g1);
[*]y.setGrade(g2);

[*]Teacher z;

[*]cout << z.calculateGrade(x) << endl;
[*]//cout << z.calculateGrade(y) << endl;

[*]//if(x==y)

[*]//cout << " Students x and y have the same grade !" << endl;
[*]//else
[*]//cout << " Students x and y don’t have the same grade !" << endl;

[*]return 0;


[*]}
[/LIST]

line 4: delete it because its redundent. Its not necessary to predeclared the class if your going to fully define it in the very next line.

line 13: As written that function does nothing. YOu need to add a bit of code that sets array Grade = array G.

line 18: that operator is not written correctly. Student x must be passed by reference, not by value, and y must have a data type. Its also missing ) at the end of the if statement

line 19: Teacher has not been declared yet, so the compiler will error on that line, which is probably the error you mentioned in your description post. Move the declaration of Teacher beginning on line 21 up above the declaration of Student and it will probably compile ok.

Lines 30-33: I don't think you can do it here even though it is declared a friend class. The compiler doesn't have any clue there array Grade is located. Best way to resolve this is to put the code in Student::setGrade method and pass array G to that method.

I followed most of Ancient Dragon's suggestions (leaving operator for later) but still getting the error that Grade is an undeclared identifier. Tried making it a public but that did nothing (kind of expected that). The code works fine with all of the calculateGrade stuff commented out so that seems to be the trouble spot. Here's the revised code: Any help would be appreciated.

[LIST=1]
[*]#include<iostream>

[*]using namespace std;
[*]const int MAX_GRADES = 4;

[*]class Student;   //needs to let teacher know about class

[*]class Teacher
[*]{
[*]private:
[*]	int Id;
[*]	float Salary;
[*]public:
[*]	float calculateGrade(Student)
[*]	{
[*]	  	float FinalGrade;
[*]		FinalGrade = Grade[0] + Grade[1] + Grade[2] + Grade[3]; 
[*]		return FinalGrade;
[*]	}
[*]	friend class Student;
[*]	};

[*]class Student
[*]{
[*]	private:
[*]		int Id;
[*]		float FinalGrade;
[*]		float Grade[MAX_GRADES];

[*]	public:
[*]		void setId(int i){Id = i;};
[*]		void setGrade(float G[])
[*]		{
[*]			  for (int i=0;i<MAX_GRADES;i++)
[*]               cout << G[i]<< endl;
[*]			   Grade[0] = G[0];
[*]			   Grade[1] = G[1];
[*]			   Grade[2] = G[2];
[*]			   Grade[3] = G[3];
[*]			     
[*]		};
[*]	//	bool operator == (Student& x,y){if x.FinalGrade == y.FinalGrade return true; else return false;};
[*]		friend class Teacher;

[*]};

[*]int main()
[*]{
[*]float g1[] = {10.0, 20.0, 25.0, 25.0};
[*]float g2[] = {25.0, 20.0, 10.0, 25.0};
[*]Student x, y;
[*]x.setGrade(g1);
[*]y.setGrade(g2);

[*]Teacher z;

[*]cout << z.calculateGrade(x) << endl;
[*]cout << z.calculateGrade(y) << endl;

[*]//if(x==y)

[*]//cout << " Students x and y have the same grade !" << endl;
[*]//else
[*]//cout << " Students x and y don’t have the same grade !" << endl;

[*]return 0;



[*]}
[/LIST]

Why did you use list tags when you posted that code? That just makes it impossible for anyone to copy/paste into their compiler's editor. Please repost without using those tags.

Sorry AD. In another thread you added line numbers to my code so I tried to add them, but used the wrong button.

#include<iostream>

using namespace std;
const int MAX_GRADES = 4;

class Student;   //needs to let teacher know about class

class Teacher
{
private:
	int Id;
	float Salary;
public:
	float calculateGrade(Student)
	{
	  	float FinalGrade;
		//FinalGrade = Grade[0] + Grade[1] + Grade[2] + Grade[3]; 
		return FinalGrade;
	}
	//friend class Student;
	};

class Student
{
	private:
		int Id;
		float FinalGrade;
		float Grade[MAX_GRADES];

	public:
		void setId(int i){Id = i;};
		void setGrade(float G[])
		{
			  for (int i=0;i<MAX_GRADES;i++)
               cout << G[i]<< endl;
			   Grade[0] = G[0];
			   Grade[1] = G[1];
			   Grade[2] = G[2];
			   Grade[3] = G[3];
			     
		};
	//	bool operator == (Student& x,y){if x.FinalGrade == y.FinalGrade return true; else return false;};
		friend class Teacher;

};

int main()
{
float g1[] = {10.0, 20.0, 25.0, 25.0};
float g2[] = {25.0, 20.0, 10.0, 25.0};
Student x, y;
x.setGrade(g1);
y.setGrade(g2);

Teacher z;

cout << z.calculateGrade(x) << endl;
cout << z.calculateGrade(y) << endl;

//if(x==y)

//cout << " Students x and y have the same grade !" << endl;
//else
//cout << " Students x and y don’t have the same grade !" << endl;

return 0;


}

Sorry AD. In another thread you added line numbers to my code so I tried to add them, but used the wrong button.

All you have to do is add the language to the code tags

[code=cplusplus] // put your code here

[/code]

line 14: you have to specify the name of the Student object, not just the data type float calculateGrade(Student& stu) When writing inline functions like that one you must always given the parameters both data type and variable name. In this case I would make it a reference to a Student object so that the compiler doesn't have to duplicate it when it is passed.

Got it.

Needed to move the function below both class declarations in order to get the compiler to recognize Grade. operator== function is working as well. Thanks again Ancient Dragon. Here's the completed code:

#include<iostream>

using namespace std;
const int MAX_GRADES = 4;

 

class Student;

class Teacher
{
private:
	int Id;
	float Salary;
public:
	float calculateGrade(Student& s);
	
	friend class Student;
	};

class Student
{
	private:
		int Id;
		float FinalGrade;
		float Grade[MAX_GRADES];

	public:
		void setId(int i){Id = i;};
		void setGrade(float stu[])
		{
			  for (int i=0;i<MAX_GRADES;i++)
            //   cout << stu[i]<< endl;
			   Grade[0] = stu[0];
			   Grade[1] = stu[1];
			   Grade[2] = stu[2];
			   Grade[3] = stu[3];
			     
		};
	bool operator==(Student& a)
	{
		if(a.FinalGrade == FinalGrade)
			return true;
		else 
			return false;
		
	};
		friend class Teacher;

};

float Teacher::calculateGrade(Student& s)
{
		float FinalGrade;
	    FinalGrade = s.Grade[0] + s.Grade[1] + s.Grade[2] + s.Grade[3]; 
		return FinalGrade;
		
}

int main()
{
float g1[] = {10.0, 20.0, 25.0, 25.0};
float g2[] = {25.0, 20.0, 10.0, 25.0};
Student x, y;
x.setGrade(g1);
y.setGrade(g2);

Teacher z;

cout << z.calculateGrade(x) << endl;
cout << z.calculateGrade(y) << endl;

if(x==y)

cout << "Students x and y have the same grade !" << endl;
else
cout << " Students x and y don’t have the same grade !" << endl;

return 0;


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.