- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将公共(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/
我有一个应用程序,它会抛出 GKSession 并在各种条件下(连接超时、 session 失败等)创建一个新的 GKSession。不过,我遇到了内存泄漏问题,并且有时会在重新连接几次循环后崩溃。
比如我在宿主代码中有一个浮点指针 float *p 是否可以确定他指向的内存类型(设备/主机)? 最佳答案 在 UVA system 中, 运行时 API 函数 cudaPointerGetAttri
我已将项目转换为 .Net 4.0 并且以下代码不起作用: typeof(RuntimeTypeHandle).GetMethod("Allocate", BindingFlags.Instance
当我声明 char ch = 'ab' 时,ch 只包含 'b',为什么它不存储 'a'? #include int main() { char ch = 'ab'; printf("%c"
我对 Disk Sector 和 Block 有疑问。扇区是一个单位,通常为 512 字节或 1k、2k、4k 等取决于硬件。文件系统 block 大小是一组扇区大小。 假设我正在存储一个 5KB 的
假设我有 8 个人和5000 个苹果。 我想将所有苹果分发给所有 8 个人,这样我就没有苹果了。 但每个人都应该得到不同数量 将它们全部分发出去的最佳方式是什么? 我是这样开始的: let peopl
我正在构建的网站顶部有一个搜索栏。与 Trello 或 Gmail 类似,我希望当用户按下“/”键时,他们的焦点就会转到该搜索框。 我的 JavaScript 看起来像这样: document.onk
我有一小段代码: if (PZ_APP.dom.isAnyDomElement($textInputs)){ $textInputs.on("focus", function(){
我观察到以下行为。 接受了两个属性变量。 @property (nonatomic, retain) NSString *stringOne; @property (nonatomic, assign
我正在解决这样的问题 - 实现一个计算由以下内容组成的表达式的函数以下操作数:“(”、“)”、“+”、“-”、“*”、“/”。中的每个数字表达式可能很大(与由字符串表示的一样大)1000 位)。 “/
我有一组主机和一组任务。 每个主机都有 cpu、mem 和任务容量,每个任务都有 cpu、mem 要求。 每个主机都属于一个延迟类别,并且可以与具有特定延迟值的其他主机通信。 每个任务可能需要以等于或
该程序的作用:从文件中读取一个包含 nrRows 行和 nrColomns 列的矩阵(二维数组)。矩阵的所有元素都是 [0,100) 之间的整数。程序必须重新排列矩阵内的所有元素,使每个元素等于其所在
世界!我有个问题。今天我尝试创建一个代码,它可以找到加泰罗尼亚语号码。但是在我的程序中可以是长数字。我找到了分子和分母。但我不能分割长数字!此外,只有标准库必须在此程序中使用。请帮帮我。这是我的代码
我确定我遗漏了一些明显的东西,但我想在 Objective C 中创建一个 NSInteger 指针的实例。 -(NSInteger*) getIntegerPointer{ NSInteger
这个问题在这里已经有了答案: Difference between self.ivar and ivar? (4 个答案) 关闭 9 年前。
我如何将 v[i] 分配给一系列整数(v 的类型是 vector )而无需最初填充 最佳答案 你的意思是将 std::vector 初始化为一系列整数? int i[] = {1, 2, 3, 4,
我想寻求分配方面的帮助....我把这个作业带到了学校......我必须编写程序来加载一个 G 矩阵和第二个 G 矩阵,并搜索第二个 G 矩阵以获取存在数第一个 G 矩阵的......但是,当我尝试运行
我必须管理资源。它基本上是一个唯一的编号,用于标识交换机中的第 2 层连接。可以有 16k 个这样的连接,因此每次用户希望配置连接时,他/她都需要分配一个唯一索引。同样,当用户希望删除连接时,资源(号
是否有任何通用的命名约定来区分已分配和未分配的字符串?我正在寻找的是希望类似于 us/s 来自 Making Wrong Code Look Wrong ,但我宁愿使用常见的东西也不愿自己动手。 最佳
我需要读取一个 .txt 文件并将文件中的每个单词分配到一个结构中,该结构从结构 vector 指向。我将在下面更好地解释。 感谢您的帮助。 我的程序只分配文件的第一个字... 我知道问题出在函数 i
我是一名优秀的程序员,十分优秀!