prototype for “…” does not match any in class “…”


I started to pick up some c++ programming and i am learning the concept of classes and objects. So I looked on the web for some exercises I can practice on. I have read that it is good practice to have the main, header and constructor file separated and not into one long file.

I'm trying to break the following code into three separate files :

// Exercises: Classes
// Exercise 3


// Exercises: Classes
// Exercise 3


#include <iostream>
using namespace std;

class Student{

public:
char *name;
int mark1;int mark2;

Student(char* na, int ma1,int ma2){
name=na;mark1=ma1;mark2=ma2;
}



int calc_media(){return (mark1+mark2)/2;}

    void disp(){
    cout << "Student:" << name << " \n media:"<< calc_media() <<"\n";
    }

};

int main(){

    char* nam;int m1,m2;

    cout << "Enter name:";
    cin>> nam;
    cout << "Enter marks of two subjects:";
    cin>> m1;
    cin>> m2;
    Student student1(nam,m1,m2);
    student1.disp();

    return 0;
}

into the following files:

main.cpp:

#include <iostream>
#include <string>
#include "student_example.h"
using namespace std;

int main()
{
   int marc1,marc2;
   char nam;

   cout<<"Please enter the name of the student:  ";
   cin>>nam;
   cout<<"Please enter the two grades of the student"<<"\n grade one:";
   cin>>marc1;
   cout<<"Grade two";
   cin>>marc2;

   student_Example student1;
   student1.disp();

   return 0;
}

header file (student_Example.h)

#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>

class student_Example
{
    public:
        char name;
        int mark1, mark2;

        int calc_media(){
           return (mark1+mark2/2);
           }

void disp(){
           std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
        }
};

And constructor:

#include <iostream>
#include <string>

#include "student_Example.h"

student_Example::student_Example(char nam, int marc1, int marc2)
{
    name=nam;
    mark1=marc1;
    mark2=maec2;

}

Im getting the error

"error: prototype for **'student_Example::student_Example(char, int, int)' does not match any class 'student_Example'**

Any advice what may be going on here? Thanks in advance :)


Answers:


Your header file with class student_Example doesn't promise a constructor. (And seems to be missing and #endif)

#ifndef STUDENT_EXAMPLE_H
#define STUDENT_EXAMPLE_H
#include <iostream>
#include <string>

class student_Example
{
    public:
        student_Example(char nam, int marc1, int marc2); //<-- as pointed out in the error
        char name;
        int mark1, mark2;

        int calc_media(){
           return (mark1+mark2/2);
        }

       void disp(){
           std::cout<< " The student named: "<< name<< "\n has an average score equal to: " << calc_media()<<"\n";
        }



};
#endif  //<-- this too

While we are there we can use an member initialiser list in the constructor

student_Example::student_Example(char nam, int marc1, int marc2) :
    name(nam),
    mark1(marc1),
    mark2(marc2) //assume maerc2 was a typo
{    
}

Edit: Note that student_Example(char nam, int marc1, int marc2) is a declaration that you will define a constructor taking a char and two ints., which you have done in your cpp file.

You can make an object like this

student_Example example('n', 1, 2);

Without this non-default constructor, a default constructor taking no parameters would have been automatically generator for you, so you could have made an object like this:

student_Example example;

Now you have defined a constructor that will no longer happen. You either need to add this to your class, or make sure you use the constructor taking parameters.