Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Here i will demonstrate my code:
C++
Curse class:
class Curse{
 private:
  char* name;
  int mark;
 public:
  friend class Student;
  Curse();
   Curse(char*,int);
   ~Curse();
};

Curse CPP file:
C++
#include "Curse.h"
#include <string.h>
Curse::Curse(){
strcpy(this->name,"");
this->mark = 0;
}

Curse::Curse(char* name,int mark)
{
strcpy(this->;name,name);
this->mark = mark;
}

Curse::~Curse() { if(name!= NULL) delete[] name;}


--------------------------------------------------------

And I have Student class:
C++
#include <string.h>
#include "Curse.h"
class Student{
  private:
    char name[30];
    Curse *curses;
  public:
    Student(){
               strcpy(this->name,"");
                curses = new Curse[10];
                }
};


--------------------------

Main.cpp
C++
#include <iostream>
#include "Student.h"

void main()
{
   Student S1;
   S1.curses[0]->mark = 10; // HERE IS THE EROR 
   // curses[0] is inaccessible ?? PLEASE HELP
}

Please help, error is in Main.CPP??

THANKS!
Posted

curses is a private property of the Student class, so it is only accessible within methods of that class. And main does not belong to any class. This is nothing to do with friend classes. See http://msdn.microsoft.com/en-us/library/4a1hcx0y.aspx[^].
 
Share this answer
 
Comments
Stefan_Lang 2-Dec-14 8:47am    
There are lots of cursing people around, I don't think it's ever been a private property of students;

Sorry, couldn't resist ;-p
Are you already cursing C++? I suspect what you meant to name your class is "course"[^], not "curse"[^] ;-)

That said, friend should be used very sparingly in C++! it is a keyword that is sometimes necessary for strongly interdependend classes, such as container classes (e. g. std::vector) and their corresponding iterators.

For any other case, friend is a way of saying Encapsulation[^] is your enemy. But it isn't - and you should reallly learn how to work with it:

Enter the world of accessors and mutators[^]. Instead of calling every other class that may interact with your class a friend (something you can't even know when your application is a library used by a program you didn't write), provide a neat interface that lets others access your protected member variables in a controlled way:

C++
class Course {
private:
   char* name;
   int mark;
public:
   Course();
   Course(const char* course_name, const int course_mark);
   // accessor definitions:
   const char* get_name() const;
   const int get_mark() const;
   // mutator definitions:
   void set_name(const char* new_name);
   void set_mark(const int new_mark);
};

The accessor and mutator functions will allow you to read and write member variables without accessing them directly. This will allow you to control access and verify changes within the class. For example, if your Course already holds a valid name and mark, you may not want to allow overwriting it. E. g. replace the two set functions above with a single one like that:
C++
bool set(const char* course_name, const int new_mark) {
   bool result = true;
   if (name == nullptr) {
      // not set, copy name and mark
      ...
   }
   else if (strcmp(course_name, name) == 0) {
      // ok, correct course chosen, update the mark
      mark = new_mark;
   }
   else {
      // error: trying to change mark on different course
      result = false;
   }
   return result;
}

Now you have restricted the way others can change your member variables without knowing who will ever use this class.

For the class Student it's similar: you should write accessors and mutators if you want others to read and write your member variables. Feel free not to provide these for every member variable, or maybe only provide accessors for reading.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900