“......”的原型与“......”中的任何一个都不匹配

时间:2017-03-10 14:44:52

标签: c++

我开始学习一些c ++编程,我正在学习类和对象的概念。所以我在网上看了一些我可以练习的练习。我已经读过,将main,header和constructor文件分开而不是一个长文件是一种好习惯。

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

// 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'**

有什么建议吗?在此先感谢:)

2 个答案:

答案 0 :(得分:2)

您的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

当我们在那里时,我们可以在构造函数中使用member initialiser list

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)是一个声明,您将定义一个构造函数,该构造函数采用{c}文件中的char和两个int

你可以制作一个像这样的对象

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

如果没有这个非默认构造函数,不带参数的默认构造函数就会自动为你生成,所以你可以创建一个像这样的对象:

student_Example example;

现在您已经定义了一个不再发生的构造函数。您需要将其添加到您的类中,或者确保使用构造函数获取参数。

答案 1 :(得分:1)

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