3
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

class Student;
void function_A(Student& s)

class Student { 
   void function_B() {
        ::function_A(*this);
   }
   int courses;
};

void function_A(Student& s)
{ // line 18 (where error is occurring)
     s.courses = 1;
}

int main()
{
    Student s;
    s.function_B();
    return 0;
}

The error that I am getting is as follows:

(line 18) New types may not be defined in a return type.

2 Answers2

6

Part of your problem is you're using the type Student before it's defined by making it a parameter to function_A. To make this work you need to

  1. Add a forward declaration function_A
  2. Switch function_A to take a pointer or reference
  3. Move function_A after Student. This is necessary so member courses is defined before it's accessed
  4. Add a ; after the end of the class Student definition

Try the following

class Student;
void function_A(Student& s);

class Student { 
   // All of the student code
};

void function_A(Student& s) {
  s.courses = 1;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

You have to forward declare Student.

Place

 class Student;

before function_A.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 1
    A forward declaration isn't sufficient either, since the Student object is being passed by value, and function_A references a member variable of the Student class. – Jeremy Friesner Apr 29 '11 at 00:10