gpt4 book ai didi

c++ - 重新分配变量 C++

转载 作者:行者123 更新时间:2023-11-30 05:34:43 24 4
gpt4 key购买 nike

我正在尝试将公共(public)变量 overallRating 重新分配给值 7。它在构造函数中最初设置为 0。我的代码可以编译,但是当我尝试访问另一个文件中的 schoolName.overallRating 时,它说 overallRating = 0 而不是 7。我的赋值语句有问题吗? GradSchool 类仅包含学校列表。

学校.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;
}

学校.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

研究生院

#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

最佳答案

   School entry;
schools.retrieve(i, entry);
entry.computeRating(weightWomen, weightAI, weightSys, weightTheory, weightEffect, weightPubs);

虽然在您的代码中不可见,但我想 schools 是一些集合,函数 retrieve 检索数组中的一些元素。

问题是您上面的代码适用于条目的拷贝,而不是条目本身。因此,schools 集合中的元素未被修改,因为 computeRating 是在 copied entry 变量上调用的,不是元素本身。

要完全修复您的代码,我们需要查看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;
}

之后,引用的代码变为:

    School* entry;
schools.retrieve(i, entry);
entry->computeRating(weightWomen, weightAI, weightSys, weightTheory, weightEffect, weightPubs);

关于c++ - 重新分配变量 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34122566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com