"...."的原型与类中的任何"..."都不匹配

prototype for "...." does not match any in class "..."

本文关键字:不匹配 任何 原型      更新时间:2023-10-16

我开始拿起一些C 编程,并且正在学习类和对象的概念。因此,我在网上寻找一些可以练习的练习。我读到,将主,标头和构造函数分开而不是一个长文件是一个好习惯。

我正在尝试将以下代码分解为三个单独的文件:

// 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;
}

进入以下文件:

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;
}

标题文件(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";
        }
};

和构造函数:

#include <iostream>
#include <string>
#include "student_Example.h"
student_Example::student_Example(char nam, int marc1, int marc2)
{
    name=nam;
    mark1=marc1;
    mark2=maec2;
}

我得到错误

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

有任何建议在这里发生什么?预先感谢:)

带有class student_Example的标头文件不承诺构造函数。(并且似乎缺少和#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

当我们在那里时,我们可以在构造函数中使用成员启动器列表

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

编辑:请注意,student_Example(char nam, int marc1, int marc2)是一份声明,您将定义一个构造函数,采用char和两个int s。,您在CPP文件中完成了这些构建器。

您可以制作这样的对象

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

没有这个非默认构造函数,默认的构造函数将自动为您提供任何参数,因此您可以制作这样的对象:

student_Example example;

现在,您已经定义了不再发生的构造函数。您要么需要将其添加到课堂上,要么确保使用构造函数采用参数。

DoctorLove解决了您的问题,但也是.cpp文件中使用方法的好习惯,例如:

Student_example.h

#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();
        void disp();
};
#endif

Student_example.cpp

#include "student_Example.h"
student_Example::student_Example(char nam, int marc1, int marc2) :
    name(nam),
    mark1(marc1),
    mark2(marc2) //assume maerc2 was a typo
{    
}
int student_Example::calc_media(){
    return (mark1+mark2/2);
}
void student_Example::disp(){
    std::cout<< " The student named: "<< name<< "n has an average score equal to: " << calc_media()<<"n";
}