gpt4 book ai didi

c++访问头文件信息

转载 作者:行者123 更新时间:2023-11-28 05:56:17 25 4
gpt4 key购买 nike

我正在尝试访问我在头文件中声明的变量。当我尝试将 women 之类的变量分配给 0 时,我收到一条错误消息,提示“'women' 未在此范围内声明”。我不确定这是什么意思。即使它包含 school.h,我是否必须再次在 .cc 文件中声明变量?此外,我收到重载赋值运算符的错误,该错误显示为“ISO C++ 禁止声明没有类型的‘School’,预期‘,’或‘...’在‘&’标记之前,‘bool operator==(int )' 必须有一个类或枚举类型的参数。”

学校.cc

#include "school.h"

void School ()
{
women = 0;
rateAI = 0;
rateSys = 0;
rateTheory = 0;
effectiveness = 0;
ratePubs = 0;
overallRating = 0;
}

void 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;


bool operator ==(const School &x, const School &y)
{
return x.overallRating == y.overallRating;
}
bool operator >(const School &x, const School &y)
{
return x.overallRating > y.overallRating;
}
bool operator <(const School &x, const School &y)
{
x.overallRating < y.overallRating;
}
bool operator >=(const School &x, const School &y)
{
x.overallRating >= y.overallRating;
}
bool operator <=(const School &x,const School &y)
{
x.overallRating <= y.overallRating;
}
bool operator !=(const School &x, const School &y)
{
x.overallRating != y.overallRating;
}

学校.h

#include <string>
#include <iostream>
using namespace std;
#ifndef SCHOOL_H
#define SCHOOL_H
class School {
public:
string name;
string state;
int women;
int rateAI;
int rateSys;
int rateTheory;
int effectiveness;
int ratePubs;
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);
};

bool operator ==(const School &x,const School &y);
bool operator >(const School &x,const School &y);
bool operator <(const School &x,const School &y);
bool operator >=(const School &x,const School &y);
bool operator <=(const School &x,const School &y);
bool operator !=(const School &x,const School &y);
#endif

最佳答案

成员以 ClassName::member name 的形式定义。

void School () is a free function. 
School::School(){ is the definition of the constructor.

这同样适用于您的其他成员。不是你的免费运营商。

关于c++访问头文件信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34110368/

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