我正在尝试将公共变量overallRating重新分配给值7.它最初在构造函数中设置为等于0。我的代码编译,但当我尝试访问另一个文件中的schoolName.overallRating时,它表示overallRating = 0而不是7.我的赋值语句有问题吗? GradSchool课程只包含一系列学校。
School.cc
#include "school.h"
School::School()
{
women = 0;
rateAI = 0;
rateSys = 0;
rateTheory = 0;
effectiveness = 0;
ratePubs = 0;
overallRating = 0;
}
School::School (string myName, string myState, int theWomen, int myRateAI, int myRateSys,int myRateTheory, int myEffectiveness, int myRatePubs)
{
name = myName;
state = myState;
women = theWomen;
rateAI = myRateAI;
rateSys = myRateSys;
rateTheory = myRateTheory;
effectiveness = myEffectiveness;
ratePubs = myRatePubs;
overallRating = 0;
}
void School::computeRating (int weightWomen, int weightAI, int weightSys, int weightTheory, int weightEffect, int weightPubs)
{
overallRating = 7;
}
void School::printSchoolInfo ()
{
cout<<"Name: "<<name<<endl;
cout<<"State: "<<state<<endl;
cout<<"Rating of number of women PhD's: "<<women<<endl;
cout<<"Rating of AI program: "<<rateAI<<endl;
cout<<"Rating of Systems program: "<<rateSys<<endl;
cout<<"Rating of Theory program: "<<rateTheory<<endl;
cout<<"Effctiveness rating: "<<effectiveness<<endl;
cout<<"Rating of faculty publications: "<<ratePubs<<endl;
cout<<"Overall rating: "<<overallRating<<endl;
}
School.h
#include <string>
#include <iostream>
using namespace std;
#ifndef SCHOOL_H
#define SCHOOL_H
class School {
public:
string name; // name of the school
string state; // state in which school is located
int women; // rating of percentage of women PhD's (1 to 10)
int rateAI; // rating of AI program (1 to 10)
int rateSys; // rating of Computer Systems program (1 to 10)
int rateTheory; // rating of Theory program (1 to 10)
int effectiveness; // rating of effectiveness in educating research scholars
int ratePubs; // rating of impact of publications per faculty member
int overallRating; // overall rating that considers all of the above factors
School ();
School (string myName, string myState, int theWomen, int myRateAI, int myRateSys,
int myRateTheory, int myEffectiveness, int myRatePubs);
void printSchoolInfo ();
void computeRating (int weightWomen, int weightAI, int weightSys,
int weightTheory, int weightEffect, int weightPubs);
};
#endif
主要
//
// gradSchoolTest.cc
//
// code to test the creation of a GradSchools object that stores and sorts
// multiple instances of a School
#include <iostream>
#include "gradSchools.h"
#include "gradSchools.cc"
#include "school.h"
#include "school.cc"
#include "sortable_list.h"
#include "sortable_list.cc"
#include "list.h"
#include "list.cc"
using namespace std;
GradSchools makeGradSchools () {
// Note that the ratings here are somewhat arbitrary
GradSchools newSchools;
newSchools.addSchool("MIT", "Massachusetts", 5, 10, 9, 10, 10, 7);
newSchools.addSchool("Stanford", "California", 9, 8, 5, 8, 10, 9);
newSchools.addSchool("CMU", "Pennsylvania", 6, 9, 9, 7, 8, 6);
newSchools.addSchool("UC Berkeley", "California", 4, 6, 8, 9, 9, 9);
newSchools.addSchool("Cornell", "New York", 9, 5, 8, 9, 9, 8);
newSchools.addSchool("Univ. of Illinois", "Illinois", 4, 7, 7, 7, 7, 7);
newSchools.addSchool("Univ. of Washington", "Washington", 7, 5, 7, 8, 8, 8);
newSchools.addSchool("Princeton", "New Jersey", 8, 4, 5, 8, 7, 10);
return newSchools;
} //makeGradSchools
int main (void) {
GradSchools myGradSchools = makeGradSchools();
int weightWomen = 5;
int weightAI = 5;
int weightSys = 2;
int weightTheory = 0;
int weightEffectiveness = 5;
int weightPubs = 4;
myGradSchools.rankSchools(weightWomen, weightAI, weightSys, weightTheory, weightEffectiveness, weightPubs);
cout << endl;
myGradSchools.rankSchoolsFactor("AI");
myGradSchools.rankSchoolsFactor("women");
myGradSchools.printGradSchools();
return 0;
} //end main
GradSchools
#include "gradSchools.h"
/*Implement functions from gradSchools.h file here */
GradSchools::GradSchools()
{
schools.clear();
}
void GradSchools::addSchool (string name, string state, int women, int rateAI, int rateSys,int rateTheory, int effect, int ratePubs)
{
schools.insert(schools.size(), (School(name, state, women, rateAI, rateSys, rateTheory, effect, ratePubs)));
}
void GradSchools::computeRatings (int weightWomen, int weightAI, int weightSys, int weightTheory, int weightEffect, int weightPubs)
{
for (int i = 0; i < schools.size()-1; i++)
{
School entry;
schools.retrieve(i, entry);
entry.computeRating(weightWomen, weightAI, weightSys, weightTheory, weightEffect, weightPubs);
}
}
void GradSchools::rankSchools (int weightWomen, int weightAI, int weightSys, int weightTheory, int weightEffect, int weightPubs)
{
computeRatings(weightWomen, weightAI, weightSys, weightTheory, weightEffect, weightPubs);
schools.selection_sort();
for (int i = schools.size()-1; i>= 0; i--)
{
School x;
schools.retrieve(i, x);
cout<<x.name<<endl;
}
}
void GradSchools::rankSchoolsFactor (string factor)
{
}
void GradSchools::printGradSchools()
{
for (int i = 0; i < schools.size()-1; i++)
{
School entry;
schools.retrieve(i, entry);
entry.printSchoolInfo();
cout<<endl;
}
}
检索
template<class List_entry>
Error_code List<List_entry> :: retrieve(int position, List_entry &x ) const
/*Post: If 0¾ position < n, where n is the number of entries in the List,
the function succeeds: The entry at position is copied to x; all
List entries remain unchanged.
Else: The function fails with a diagnostic error code.*/
{
if (position < 0 || position >= count) {
return range_error;
}
x = entry[position];
return success;
} //retrieve
答案 0 :(得分:1)
School entry; schools.retrieve(i, entry); entry.computeRating(weightWomen, weightAI, weightSys, weightTheory, weightEffect, weightPubs);
虽然在代码中不可见,但我认为schools
是一些集合,函数retrieve
检索数组中的某些元素。
问题是上面的代码适用于条目的副本,而不是条目本身。因此,schools
集合中的元素未被修改,因为在复制的 computeRating
变量上调用了entry
,而不是元素本身。
要完全修复您的代码,我们需要了解retrieve
的工作原理,并以某种方式对其进行修改,以便我们可以为元素获取引用,而不是复制
建议:修改您的retrieve
函数,以便它向元素返回引用。换句话说,让它具有以下签名:
School& retrieve(int i);
以后,以这种方式修改引用的代码:
School& entry = schools.retrieve(i);
entry.computeRating(weightWomen, weightAI, weightSys, weightTheory, weightEffect, weightPubs);
看到你的检索功能后编辑,如果确实需要将结果作为 out参数,你仍然可以
Error_code List<List_entry> :: retrieve(int position, List_entry*& x ) const // set the output param as a pointer
{
if (position < 0 || position >= count) {
return range_error;
}
x = &entry[position]; // return a pointer
return success;
}
以后,引用的代码变为:
School* entry;
schools.retrieve(i, entry);
entry->computeRating(weightWomen, weightAI, weightSys, weightTheory, weightEffect, weightPubs);