C++ 类“未声明的标识符”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11132930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Class 'undeclared identifier"
提问by conman
This is really frustrating, I've tried multiple variations of my class and constructor, sifted through pages of google results and other questions on this site but I can't figure it out. I believe the error is something simple that I'm overlooking.
这真的令人沮丧,我尝试了我的类和构造函数的多种变体,筛选了谷歌结果页面和本网站上的其他问题,但我无法弄清楚。我相信这个错误很简单,我忽略了。
Error code:
错误代码:
1>main.cpp(60): error C2065: 'Student' : undeclared identifier
1>main.cpp(60): error C2146: syntax error : missing ';' before identifier 'newStudent'
1>main.cpp(60): error C3861: 'newStudent': identifier not found
student.h
学生.h
#ifndef STUDENT_H
#define STUDENT_H
class Student {
private:
static const int SIZE = 7; // initial size of the array
int *Students; // Students will point to the dynamically allocated array
//int numStudents; // number of students currently in the list
int rosterSize; // the current size of the roster
int student_id;
string name;
string classification;
public:
Student(); // constructor; initialize the list to be empty
~Student();
void set_id(int);
void set_name(string);
void set_class(string);
int print_id();
string print_name();
string print_class();
//void enrollStudent();
//void Print(ostream &output) const; // print the list to output
};
#endif
student.cpp
学生.cpp
#include <iostream>
#include <string>
#include "student.h"
#define PROMPT "class> "
using namespace std;
Student::Student(){ // declared here right?
student_id = 0;
name = "";
classification = "";
}
Student::~Student(){
//delete Student
}
void Student::set_id( int i ){
student_id = i;
}
void Student::set_name( string n ){
name = n;
}
void Student::set_class( string c ){
classification = c;
}
int Student::print_id(){
return student_id;
}
string Student::print_name(){
return name;
}
string Student::print_class(){
return classification;
}
main.cpp
主程序
#include <iostream>
#include <string>
#include "student.h"
#define PROMPT "class> "
using namespace std;
//**** Implement Error Handling ****\
enum errorType {
UNKNOWN_ERROR,
INPUT_ERROR,
HANDLER,
NUM_ERRORS
};
// error messages
string errorMessage[NUM_ERRORS]= {
"Unknown Error\n",
"Input Error\n",
};
// error handler
void handleError(errorType err) {
if(err > 0 && err < NUM_ERRORS)
cout<< "Error: "<< errorMessage[err];
else cout<< "Error: "<< errorMessage[UNKNOWN_ERROR];
}
//**** END Error Handling ****\
void getInput() {
int id; string n, c;
cin>>id>>n>>c;
//cout<<id<<"-"<<n<<"-"<<c<<endl;
Student newStudent(); //****WORK DAMN U!
//set_id(id);
//print_id();
return;
}
int main() {
//Student newStudent(); /* <-- why doesn't this work?!*/
string input = "";
bool finished = false;
cout<<PROMPT; // prompt the user
while(!finished) {
if(input!="") cout<<PROMPT;
cin>>input;
if(input=="enroll") {
cout<<PROMPT<<"Enroll student:"<<endl;
getInput();
}
else if(input=="drop") {
cout<<PROMPT<<"Enter ID:"<<endl;
}
else if(input=="roster") {
cout<<"This will print formatted list of students"<<endl;
}
else if(input=="quit") {
finished=true;
}
else handleError(errorType(1));
}
}
Just as a note, my professor said adding
#include student.h
to my main.cpp
would fix my issue. OBVIOUSLY NOT because I included it in my student.cpp SO THANKS. ?_?
作为说明,我的教授说将 #include student.h 添加
到我的 main.cpp 可以解决我的问题。显然不是因为我将它包含在我的 student.cpp 中,所以谢谢。?_?
tl;dr here is a smaller barebones version that gets same errors:
tl; dr 这是一个较小的准系统版本,它会出现相同的错误:
#include<iostream>
#include<string>
#define PROMPT "class> "
using namespace std;
class Student {
private:
int student_id;
string name;
string classification;
public:
Student(); // constructor
~Student();
void set_id(int);
int print_id();
};
Student::Student(){
student_id = 0;
name = "";
classification = "";
}
Student::~Student(){
//delete Student
}
void Student::set_id( int i ){
student_id = i;
}
int Student::print_id(){
return student_id;
}
void messenger(){
int id;
Student newStudent();
cin>>id; //this will drop the name and class
newStudent.set_id(id);
newStudent.print_id();
return;
}
void main() {
//Student newStudent();
string input="";
bool finished=false;
cout<<PROMPT; // prompt the user
while(!finished) {
if(input!="") cout<<PROMPT;
cin>>input;
if(input=="enroll") {
cout<<PROMPT<<"Enroll student:"<<endl;
messenger();
//cin>>input;
//newStudent.enroll(input );
}
else if(input=="drop") {
cout<<PROMPT<<"Enter ID:"<<endl;
}
else if(input=="roster") {
cout<<"This will print formatted list of students"<<endl;
}
else if(input=="quit") {
finished=true;
}
}
}
回答by duselbaer
Have you tried to initialize the Student object without the parentheses?
您是否尝试过在没有括号的情况下初始化 Student 对象?
Student newStudent;
This calls the default constructor. What you did with
这将调用默认构造函数。你做了什么
Student newStudent();
is to declare a function which takes no parameters and returns a Student object.
是声明一个不带参数并返回一个Student对象的函数。
回答by Alok Save
Just as a note, my professor said adding
#include student.h
to mymain.cpp
would fix my issue. OBVIOUSLY NOT because I included it in mystudent.cpp
只是作为说明,我的教授说添加
#include student.h
到我main.cpp
会解决我的问题。显然不是因为我把它包含在我的student.cpp
Your main()
needs to know what is the the type Student
to be able to create its object.
So your professor is correct.
您main()
需要知道Student
能够创建其对象的类型是什么。
所以你的教授是对的。
Each source file is compiled separately, So when the compiler compiles main.cpp
it needs to seethe definition of the type Student
.It can only do so if you include the header student.
which defines the type in main.cpp
. Note that including the header in student.cpp
has no bearing on the fact that the definition needs to be seen in main.cpp
because both of them are compiled as separate files.
每个源文件都是单独编译的,所以编译器编译main.cpp
时需要查看类型的定义。Student
只有student.
在main.cpp
. 请注意,包含头文件student.cpp
与需要查看定义的事实无关,main.cpp
因为它们都被编译为单独的文件。