C++ 重新分配变量c++;
我试图将公共变量overallRating重新指定为值7。它最初在构造函数中设置为0。我的代码可以编译,但当我尝试访问另一个文件中的schoolName.overallRating时,它会显示overallRating=0而不是7。我的任务书有什么问题吗?研究生班只包含一个学校列表 School.ccC++ 重新分配变量c++;,c++,variable-assignment,C++,Variable Assignment,我试图将公共变量overallRating重新指定为值7。它最初在构造函数中设置为0。我的代码可以编译,但当我尝试访问另一个文件中的schoolName.overallRating时,它会显示overallRating=0而不是7。我的任务书有什么问题吗?研究生班只包含一个学校列表 School.cc #include "school.h" School::School() { women = 0; rateAI = 0; rateSys = 0; rateTheor
#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”
学校:学校
{
女性=0;
比率ai=0;
rateSys=0;
比率理论=0;
有效性=0;
ratePubs=0;
总负荷=0;
}
学校::学校(字符串myName,字符串myState,int theWomen,int myRateAI,int myRateSys,int myRateTheory,int MyEffectiveity,int myRatePubs)
{
name=myName;
状态=我的状态;
女人=女人;
rateAI=myRateAI;
rateSys=myRateSys;
比率理论=我的比率理论;
有效性=有效性;
ratePubs=myRatePubs;
总负荷=0;
}
无效学校:计算机化(女性体重指数、AI体重指数、SYS体重指数、理论体重指数、效果体重指数、酒吧体重指数)
{
总负荷=7;
}
无效学校::printSchoolInfo()
{
库特
虽然在代码中不可见,但我认为schools
是某个集合,函数retrieve
检索数组中的某个元素
问题是上面的代码是在条目的副本上工作的,而不是条目本身。因此,schools
集合中的元素不会被修改,因为computeRating
是在copied条目
变量上调用的,而不是元素本身
要完全修复您的代码,我们需要了解retrieve
如何工作,并以某种方式对其进行修改,以便获得对元素的引用,而不是副本
建议:修改retrieve
函数,使其返回对元素的引用。换句话说,使其具有以下签名:
School& retrieve(int i);
然后,通过以下方式修改引用的代码:
School& entry = schools.retrieve(i);
entry.computeRating(weightWomen, weightAI, weightSys, weightTheory, weightEffect, weightPubs);
编辑查看检索函数后,如果它确实需要将结果作为输出参数,您仍然可以
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;
}
在主printSchoolInfo和Computering中反转。这可能很愚蠢,但是,在检查总体评分值是否已更改之前,是否调用Computering
?检查是否已更改是什么意思?在调用printSchoolInfo()之前,我调用Computering().@Steve,但你没有在任何地方显示。请按照投票结束时的建议发布一个MCVE。谢谢,我添加了检索功能。@Steve看到了我关于修改检索
函数的建议。事实上,你似乎返回了一个错误代码而不是元素引用,我看不到你在哪里检查此代码。你为什么需要是这样吗?但是如果你坚持这样做,你需要使用一个指针。请参阅我的编辑以获得一个保持函数逻辑(返回代码)的解决方案,尽管我认为这不是推荐的设计模式(考虑在索引超出范围时抛出异常,而不是返回错误代码)。
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
School entry;
schools.retrieve(i, entry);
entry.computeRating(weightWomen, weightAI, weightSys, weightTheory, weightEffect, weightPubs);
School& retrieve(int i);
School& entry = schools.retrieve(i);
entry.computeRating(weightWomen, weightAI, weightSys, weightTheory, weightEffect, weightPubs);
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);