我在这段代码中遇到了以下问题。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
//////////////////////////////////////////////////////////
//////////////////////////CORE CLASS//////////////////////
//////////////////////////////////////////////////////////
class Core {
public:
Core() : midterm(0), final(0) { }
Core(std::istream& is) { read(is); }
std::string name() const;
virtual std::istream& read(std::istream&);
virtual double grade() const;
protected:
std::istream& read_common(std::istream&);
std::istream& read_hw(std::istream&, std::vector<double>&);
double midterm, final;
std::vector<double> homework;
private:
std::string n;
};
std::string Core::name() const { return n; }
double Core::grade() const
{
return calc_grade(midterm, final, homework);
}
std::istream& Core::read_common(std::istream& in)
{
in >> n >> midterm >> final;
return in;
}
std::istream& Core::read(std::istream& in)
{
read_common(in);
read_hw(in, homework);
return in;
}
std::istream& Core::read_hw(std::istream& in, std::vector<double>& homework)
{
double input;
while (in >> input)
homework.push_back(input);
return in;
}
//////////////////////////////////////////////////////////
//////////////////////////GRAD CLASS//////////////////////
//////////////////////////////////////////////////////////
class Grad : public Core {
public:
Grad() : thesis(0) { }
Grad(std::istream& is) { read(is); }
double grade() const;
std::istream& read(std::istream&);
private:
double thesis;
};
std::istream& Grad::read(std::istream& in)
{
read_common(in);
in >> thesis;
read_hw(in, homework);
return in;
}
double Grad::grade() const
{
return std::min(Core::grade(), thesis);
}
//////////////////////////////////////////////////////////
//////////////////////MISCELLANEOUS///////////////////////
//////////////////////////////////////////////////////////
bool compare_grades(const Core& c1, const Core& c2)
{
return c1.grade() < c2.grade();
}
bool compare_Core_ptrs(const Core* cp1, const Core* cp2)
{
return compare_grades(*cp1, *cp2);
}
double calc_grade(double midterm, double final, std::vector<double>& homework)
{
double result = 0.0;
for (std::vector<double>::const_iterator i = homework.begin(); i != homework.end(); ++i)
result += *i;
return midterm*0.4 + final*0.4 + (result / homework.size())*0.2;
}
using namespace std;
int main()
{
vector<Core*> students;
Core* record;
char ch;
string::size_type maxlen = 0;
while (cin >> ch)
{
if (ch == 'U')
record = new Core;
else
record = new Grad;
record->read(cin);
maxlen = max(maxlen, record->name().size());
students.push_back(record);
}
sort(students.begin(), students.end(), compare_Core_ptrs);
for (vector<Core*>::size_type i = 0; i != students.size(); ++i)
{
cout << students[i]->name() << string(maxlen + 1 - students[i]->name().size(), ' ');
try
{
double final_grade = students[i]->grade();
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade << setprecision(prec) << endl;
}
catch (domain_error e)
{
cout << e.what() << endl;
}
delete students[i];
}
return 0;
}
错误C3861:&#39; calc_grade&#39;:找不到标识符。我尝试在类之前移动函数但没有任何改变。我不明白什么是错的。帮助
答案 0 :(得分:2)
您需要将calc_grade移动到等级之上,以便它知道它存在。或者像上面这样声明它。
double calc_grade(double midterm, double final, std::vector<double>& homework);
您的成绩方法标记为常量,因此可能不会更改您的成绩。但是你正在调用calc_grade,它不会采用const向量,因为它可以改变你正在处理它的类成员。你可以使用const向量来解决这个问题。我也会把这个方法设为const以保持一致。
一种可能的解决办法:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
//////////////////////////////////////////////////////////
//////////////////////////CORE CLASS//////////////////////
//////////////////////////////////////////////////////////
class Core {
public:
Core() : midterm(0), final(0) { }
Core(std::istream& is) { read(is); }
std::string name() const;
virtual std::istream& read(std::istream&);
virtual double grade() const;
protected:
std::istream& read_common(std::istream&);
std::istream& read_hw(std::istream&, std::vector<double>&);
double midterm, final;
std::vector<double> homework;
private:
std::string n;
};
std::string Core::name() const { return n; }
double calc_grade(double midterm, double final, const std::vector<double>& homework)
{
double result = 0.0;
for (std::vector<double>::const_iterator i = homework.begin(); i != homework.end(); ++i)
result += *i;
return midterm*0.4 + final*0.4 + (result / homework.size())*0.2;
}
double Core::grade() const
{
return calc_grade(midterm, final, homework);
}
std::istream& Core::read_common(std::istream& in)
{
in >> n >> midterm >> final;
return in;
}
std::istream& Core::read(std::istream& in)
{
read_common(in);
read_hw(in, homework);
return in;
}
std::istream& Core::read_hw(std::istream& in, std::vector<double>& homework)
{
double input;
while (in >> input)
homework.push_back(input);
return in;
}
//////////////////////////////////////////////////////////
//////////////////////////GRAD CLASS//////////////////////
//////////////////////////////////////////////////////////
class Grad : public Core {
public:
Grad() : thesis(0) { }
Grad(std::istream& is) { read(is); }
double grade() const;
std::istream& read(std::istream&);
private:
double thesis;
};
std::istream& Grad::read(std::istream& in)
{
read_common(in);
in >> thesis;
read_hw(in, homework);
return in;
}
double Grad::grade() const
{
return std::min(Core::grade(), thesis);
}
//////////////////////////////////////////////////////////
//////////////////////MISCELLANEOUS///////////////////////
//////////////////////////////////////////////////////////
bool compare_grades(const Core& c1, const Core& c2)
{
return c1.grade() < c2.grade();
}
bool compare_Core_ptrs(const Core* cp1, const Core* cp2)
{
return compare_grades(*cp1, *cp2);
}
using namespace std;
int main()
{
vector<Core*> students;
Core* record;
char ch;
string::size_type maxlen = 0;
while (cin >> ch)
{
if (ch == 'U')
record = new Core;
else
record = new Grad;
record->read(cin);
maxlen = max(maxlen, record->name().size());
students.push_back(record);
}
sort(students.begin(), students.end(), compare_Core_ptrs);
for (vector<Core*>::size_type i = 0; i != students.size(); ++i)
{
cout << students[i]->name() << string(maxlen + 1 - students[i]->name().size(), ' ');
try
{
double final_grade = students[i]->grade();
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade << setprecision(prec) << endl;
}
catch (domain_error e)
{
cout << e.what() << endl;
}
delete students[i];
}
return 0;
}