"...."的原型与类中的任何"..."都不匹配
prototype for "...." does not match any in class "..."
我开始拿起一些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";
}
相关文章:
- 错误:'EM::EM(...)' 的原型与类 'EM' 中的任何原型都不匹配
- 显式专用化 - 模板 ID 与任何模板声明都不匹配
- 模板类专用化:模板 ID 与任何模板声明都不匹配
- 模板 ID 与任何模板声明都不匹配
- 我的 Boost 正则表达式与任何内容都不匹配
- template-id 与任何模板声明 GNU gcc 编译器都不匹配
- 匹配匹配任何内容,直到'%'不后跟'%',后跟一组字符
- 错误:'void TNode::inititTree(TNode*)' 的原型与类 'TNode' 中的任何原型都不匹配
- Qt:"closeEvent"的外线定义与"MainWindow"中的任何声明都不匹配
- 错误:原型与类中的任何原型都不匹配
- "...."的原型与类中的任何"..."都不匹配
- qregexp不匹配任何东西
- 你能帮助解决这个错误吗?(g++原型与类中的任何原型都不匹配)
- 函数的原型与类C++中的任何原型都不匹配
- 为什么这些函数模板中的任何一个都与实例化不匹配?
- 'function'的外联定义与'Class'中的任何声明都不匹配
- C++11正则表达式不匹配任何内容
- 为什么regex_search不匹配任何东西
- c++模板函数错误:template-id不匹配任何模板声明
- 模板专门化:不匹配任何模板声明