我是C ++的新手。我正在尝试在Visual Studio上编写程序,但是我在解密编译器错误方面遇到了一些麻烦。我真的可以帮助找出如何正确调试和编译程序,以便将来编译代码时没有太多麻烦。
我有三个文件:Gradebook.h,Gradebook.cpp和Source1.cpp。 Gradebook.h位于头文件中,另外两个位于解决方案资源管理器中的源文件中。
编辑:我有一堆语法错误和其他不必要的注释让我很难阅读自己的代码。 (感谢雷,以及其他所有人)。以下是我修改后的代码。我还发现了如何正确使用代码示例工具,所以现在一切都应该正确缩进。 po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("diff,d", po::value< std::string >(),"Specify the .xyz file, name of the .xyz to create")**.xyz file I want to validate,while givin input as comman line **
;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
我在这里收到的主要错误如下:
#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: \n\n";
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”匹配指定的类型。
GradeBook:: GradeBook(string name, const int gradesArray[][GradeBook::tests])
错误:缺少显式类型(假设为“int”)&amp;错误:声明与“std :: string Gradebook”(在第12行声明)不兼容。
我真的很困惑和沮丧。你能提供的任何见解都会非常有帮助。这是我教科书中的一个例子,几乎是逐字记录的,我在过去的两个小时里一直试图找出遗漏的东西。
我的最后一个源文件如下:
GradeBook::Gradebook(string, const int[][tests])
{
}
我在最后几行收到错误: 成绩簿myGradebook(“xxxx”)收到ERROR:期望“;” ..但我不是已经有了吗?
和myGradebook.displayMessage();错误:标识符“myGradeBook”未定义。
请告知并指出正确的方向。我迫切需要帮助。
答案 0 :(得分:1)
您必须真正改进此代码。
错误
//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()
答案 1 :(得分:0)
问题出在您的.h文件中。您使用构造函数名称输入了拼写错误。你必须改变
Gradebook(string, const int[][tests]);
到
string GradeBook(string, const int[][tests]);
此外,由于您正在访问类外部的tests变量,因此编译器并不了解您所指的测试。所以,你必须使用
GradeBook::tests
答案 2 :(得分:0)
我在您发布的代码中注意到的一些问题:
//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;
}
错误说ERROR:标识符&#34;测试&#34;未定义。
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已在&#34; Gradebook.h&#34; ??
中定义
该方法在头文件中不是定义,它是声明的。不同之处在于定义包含一组代码,而声明则不包含。换句话说,method_name();
是一个声明,method_name() { /* empty body */ }
是一个定义。
由于拼写错误,您还会遇到一些其他语法错误。您的姓名为GradeBook
,而不是Gradebook
,因为您已在下方编码。
//perform various operations on the data
void Gradebook::processGrades()
{
outputGrades(); //output grades array
// ...
}
错误:缺少显式类型(&#39; int&#39;假设)&amp;错误:声明 与&#34; std :: string Gradebook不兼容(在第12行声明)。
出现此错误的原因是您在头文件GradeBook.h
中声明了ctor的方式:
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,第二个是第一个下面的几行,如下所示:
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。如果没有它,你会想知道你是如何尝试过代码的。
答案 3 :(得分:0)
First error:
void GradeBook::setCourseName(string name
{
courseName = name;
})//end function setCourseName
Error2:
int GradeBook::getMaximum)