C++编译器无法识别头文件
C++ compiler not recognizing header file
我是C++新手。我正在尝试练习在Visual Studio上编译程序,但是在破译编译器错误时遇到了一些麻烦。我真的可以使用一些帮助来弄清楚如何正确调试和编译程序,这样我将来编译代码就不会有太多麻烦。
我有三个文件:Gradebook.h,Gradebook.cpp和Source1.cpp。Gradebook.h 位于头文件中,另外两个位于解决方案资源管理器的源文件中。
编辑:我有一堆语法错误和其他不必要的注释,这使我难以阅读自己的代码。(感谢雷和其他所有人(。以下是我修改后的代码。我还发现了如何正确使用代码示例工具,所以现在一切都应该正确缩进。
#include <string>
using namespace std;
class GradeBook
{
public:
//constants
static const int students = 10; //number of tests
static const int tests = 3; //number of tests
//constructor initializes course name and array of grades
string Gradebook(string, const int[][tests]);
void setCourseName(string); //function to set course name
string getCourseName(); //function to retrieve the course name
void displayMessage(); //display a welcome message
void processGrades(); //perform various operations on the grade data
int getMinimum(); //find the minimum grade in the grade book
int getMaximum(); //find the maximum grade in the grade book
double getAverage(const int[], const int); // get student's average
void outputBarChart(); //output bar chart of grade dist
void outputGrades(); //output the contents of the grades array
private:
string courseName; //course name for this gradebook
int grades[students][tests]; //two-dimensional array of grades
}; //end class GradeBook
I don't get any errors in Gradebook.h, but the main problem lies in my other source files:
#include <iostream>
#include <iomanip>
using namespace std;
//include definition of class GradeBook from GradeBook.h
#include "GradeBook.h"
// two-argument constructor initializes courseName and grades array
GradeBook:: GradeBook(string name, const int gradesArray[][GradeBook::tests])
{
setCourseName(name); //initialize coursename
//copy grades from gradeArray to grades
for (int student = 0; student < students; ++student)
for (int tests = 0; tests < tests; ++tests)
grades[student][tests] = gradesArray[student][tests];
} //end two argument GradeBook constructor
//function to set the course name
void GradeBook::setCourseName(string name)
{
courseName = name;
}
GradeBook::Gradebook(string, const int[][tests])
{
}
void GradeBook::setCourseName(string)
{
}
//function to retrieve the course name
string GradeBook::getCourseName()
{
return courseName;
}
//display a welcome message to GradeBook user
void GradeBook::displayMessage()
{
//statements calls getCourseName to get the name of the course the gradebook represents
cout << "Welcome to the grade book for n" << getCourseName() << "!"
<< endl;
}
//perform various operations on the data
void GradeBook::processGrades()
{
outputGrades(); //output grades array
// call functions getMinimum and getMaximum
cout << "nLowest grade in the grade book is " << getMinimum()
<< "nHighest grade in the grade book is " << getMaximum() << endl;
outputBarChart(); //display distribution chart of grades on all tests
}
//find minimum grade in the entire Gradebook
int GradeBook::getMinimum()
{
int lowGrade = 100; //assume lowest grade is 100;
//loop through rows of grades array
for (int student = 0; student < students; ++student)
{
//loop to columns of current row
for (int tests = 0; tests < tests; ++tests)
{
//if current grade less than lowGrade, assign it to lowGrade
if (grades[student][tests] < lowGrade)
lowGrade = grades[student][tests]; //new lowest grade
}
}
return lowGrade;
}
//find maximum grade in the entire gradebook
int GradeBook::getMaximum()
{
int highGrade = 0; //assume highest grade is 0
for (int student = 0; student < students; ++student)
{
//loop to columns of current row
for (int tests = 0; tests < tests; ++tests)
{
//if current grade less than highGrade, assign it to highGrade
if (grades[student][tests] > highGrade)
highGrade = grades[student][tests]; //new highest grade
}
}
return highGrade;
}
//determine average grade for particular set of grades
double GradeBook::getAverage(const int setOfGrades[], const int grades)
{
int total = 0; //initialize total
//sum grades in array
for (int grade = 0; grade < grades; ++grade)
total += setOfGrades[grade];
//return average of grades
return static_cast <double>(total) / grades;
}
//output bar chart displaying grade distribution
void GradeBook::outputBarChart()
{
cout << "nOverall grade distribution: " << endl;
//stores frequency of grades in each range of 10 grades
const int frequencySize = 11;
int frequency[frequencySize] = {}; //initalize elements to 0;
//for each grade, increment the appropriate frequency
for (int student = 0; student < students; ++student)
for (int tests = 0; tests < tests; ++tests)
++frequency[grades[student][tests] / 10];
//for each grade frequency, print bar in chart
for (int count = 0; count < frequencySize; ++count)
{
//output bar label (0-9, 90-99, 100)
if (count == 0)
cout << "0-9: ";
else if (count == 10)
cout << "100: ";
else
cout << count * 10 << "-" << (count * 10) + 9 << ": ";
//print bar of asterisks
for (int stars = 0; stars < frequency[count]; stars)
cout << '*';
cout << endl;
}
}
//output the contents of the grades array
void GradeBook::outputGrades()
{
cout << "nThe grades are: nn";
cout << " "; //align column heads
//create a column heading for each of the tests
for (int tests = 0; tests < tests; ++tests)
cout << "Test" << tests + 1 << " ";
cout << "Average" << endl; //student average column heading
//create rows/columns of text representing array grades
for (int student = 0; student < students; ++student)
{
cout << "Student " << setw(2) << student + 1;
//output student's grades
for (int tests = 0; tests < tests; ++tests)
cout << setw(8) << grades[student][tests];
//call member function getAverage to calculate student's average
//pass row of grades and the value of tests as the arguments
double average = getAverage(grades[student], tests);
cout << setw(9) << setprecision(2) << fixed << average << endl;
}
}
我在这里收到的主要错误如下:
GradeBook:: GradeBook(string name, const int gradesArray[][GradeBook::tests])
错误:没有重载函数"成绩簿::成绩簿"的实例与指定的类型匹配。
GradeBook::Gradebook(string, const int[][tests])
{
}
错误:显式类型缺失(假定为"int"(和错误:声明与"std::string Gradebook(在第 12 行声明("不兼容。
我真的很困惑和沮丧。您可以提供的任何见解都将非常有帮助。这是我教科书中的一个例子,几乎是逐字逐句的,在过去的两个小时里,我一直在试图找出缺少什么。
我拥有的最后一个源文件如下:
#include "Gradebook.h"
#include "Source1.h"
//function main begins program execution
int main()
{
//two-dimensional array of student grades
int gradesArray[ GradeBook::students][GradeBook::tests] =
{
{87, 96, 70},
{68, 87, 90},
{94, 100, 90},
{100, 81, 82},
{83, 65, 85},
{78, 87, 65},
{85, 75, 83},
{91, 94, 100},
{76, 72, 84},
{87, 93, 73}
}
GradeBook myGradebook("CS101 Introduction to C++ Programming", gradesArray);
myGradeBook.displayMessage();
myGradeBook.processGrades();
} //end main
我在最后几行收到错误:成绩簿 myGradebook("xxxx"( 收到错误:预期为";" ..但是我不是已经有一个了吗?
和 myGradebook.displayMessage((;错误:标识符"我的成绩簿"未定义。
请指教并指出我正确的方向。我迫切需要帮助。
你必须真正改进这段代码。
错误
//function to set the course name
void GradeBook::setCourseName(string name
{
courseName = name;
})//end function setCourseName
正确
//function to set the course name
void GradeBook::setCourseName(string name)
{
courseName = name;
}//end function setCourseName
====
错误
//find maximum grade in the entire gradebook
int GradeBook::getMaximum)
正确
//find maximum grade in the entire gradebook
int GradeBook::getMaximum()
我在您发布的代码中注意到的一些问题:
括号中的错别字
//function to set the course name
void GradeBook::setCourseName(string name
{
courseName = name;
})//end function setCourseName
末尾有一个带有括号的错别字,由于不必要的// end function
注释,您可能更难注意到。它应该是:
void GradeBook::setCourseName(string name)
{
courseName = name;
}
未定义的变量
错误提示错误:标识符"测试"未定义。
for (int student = 0; student < students; ++student)
for (int test = 0; tests < tests; ++test)
grades[student][test] = gradesArray[student][test];
}
它应该是:for (int test = 0; /* not tests */ test < GradeBook::tests; ++test)
定义与声明
但是 setCourseName 不是已经在"成绩簿.h"中定义了吗?
该方法未在头文件中定义,而是声明的。区别在于定义包含代码主体,而声明不包含。换句话说,method_name();
是一个声明,method_name() { /* empty body */ }
是一个定义。
类名无效 - 区分大小写
由于拼写错误,您还存在一些其他语法错误。类名是 GradeBook
,而不是您在下面编码的Gradebook
。
//perform various operations on the data
void Gradebook::processGrades()
{
outputGrades(); //output grades array
// ...
}
无效的 CTOR 声明
错误:显式类型缺失(假定为"int"(和错误:声明 与"std::string Gradebook(在第 12 行声明("不兼容。
此错误的原因是您在头文件中声明 ctor 的方式,GradeBook.h
:
string Gradebook(string, const int[][tests]);
标头中的构造函数声明在 2 个方面是错误的:
- 名称应与类的名称匹配,即
GradeBook
(大写字母 B(,以及 - 构造函数没有返回类型
string
或任何其他基元。它应该是GradeBook(string, const int[][tests]);
的,因为它以类型的形式返回自身的实例。
简而言之,它应该如下所示:
Gradebook(string, const int[][tests]); // does not return std::string
重复的 ctor 定义
您已经定义了两次相同的 ctor,第二个是第一个下的几行,如下所示:
GradeBook::Gradebook(string, const int[][tests])
{
}
您应该删除其中一个。
括号中的错别字 - 续集
这行代码
int GradeBook::getMaximum)
应该是
int GradeBook::getMaximum()
// ^ note this guy
其他提示
提示 1:有时一个语法错误会级联并导致编译器列出一堆其他非错误。一旦你修复了一个,其他几个也可能消失,所以尽量经常重建。
提示 2:尝试在你的东西工作时使用非静态常量。声明和初始化静态可能比替代方法稍微复杂一些。
提示3:可能没有理由让static
成员作为public
。不要发布任何不需要的内容,如果需要,请考虑改用方法。
提示 4:考虑减少不必要的评论噪音。您实际上不需要在每次关闭大括号时添加注释以指示您要结束某个部分。写a = a + 1;
并添加一条评论说// add 1 to variable 'a'
同样明显。只记录不明显的事情。
提示 5:避免在尝试第一次构建之前尝试编写所有代码。相反,一旦您有一部分代码应该可以工作,就按下构建按钮,并在实际工作后继续。
提示 6:考虑使用源代码管理管理工具,如 Git。你会想知道你是如何尝试在没有它的情况下处理代码的。
问题出在您的 .h 文件上。你用构造函数名称做了一个拼写错误。你必须改变
Gradebook(string, const int[][tests]);
自
string GradeBook(string, const int[][tests]);
此外,由于您正在访问类外部的测试变量,因此编译器无法理解您引用的测试。所以,你必须使用
GradeBook::tests
First error:
void GradeBook::setCourseName(string name
{
courseName = name;
})//end function setCourseName
Error2:
int GradeBook::getMaximum)
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 如何识别项目是 QT 中的文件还是文件夹
- 无法识别 Mac c++ 文件系统库
- 我的头文件 DNode 中的我的数据类型未被识别为数据类型
- G++ 编译器无法识别 SQLAPI.h 头文件
- 生成库失败:无法识别文件格式;作为链接器脚本处理
- 从文件读取时"n"无法识别
- 如何让 + 重载在另一个文件C++中被识别?
- /usr/bin/ld: build-debug/obj/gpsitem.o: 文件无法识别: 文件被截断
- Visual Studio 停止识别任何包含文件
- 对生成文件无法识别的文件的更改
- 如何从核心转储文件中识别导致崩溃的完整命令
- MySQL C++连接器:添加符号时出错:文件格式无法识别
- 在另一个文件中定义函数后,Visual Studio 无法识别该函数
- 在头文件中使用时无法识别结构
- 为什么C 模板功能无法在同一标头文件中识别功能
- 从文件阅读时未识别的字符
- /usr/bin/ld找不到标头:未识别的文件格式
- TensorFlow模型:如何从原始爱好文件中识别输入/输出节点名称
- 如何使CLion将.c文件识别为c++(而不是纯c)