c++访问头文件信息
c++ accessing header file information
我正在尝试访问我在头文件中声明的变量。当我尝试将 women 之类的变量分配给 0 时,我收到一条错误消息:“'women' 未在此范围内声明。”我不确定这是什么意思。即使它包含 school.h,我是否必须再次在 .cc 文件中声明变量?
另外,我收到一个关于我的重载赋值运算符的错误,它显示为 "ISO C++ forbids declaration of ‘School’ with no type, expected ‘,’ or ‘...’ before ‘&’ token, ‘bool operator==(int)’ must have an argument of class or enumerated type."
school.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;
}
school.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 名称的形式定义。
void School () is a free function.
School::School(){ is the definition of the constructor.
这同样适用于您的其他成员。不是你的免费运营商。
我正在尝试访问我在头文件中声明的变量。当我尝试将 women 之类的变量分配给 0 时,我收到一条错误消息:“'women' 未在此范围内声明。”我不确定这是什么意思。即使它包含 school.h,我是否必须再次在 .cc 文件中声明变量? 另外,我收到一个关于我的重载赋值运算符的错误,它显示为 "ISO C++ forbids declaration of ‘School’ with no type, expected ‘,’ or ‘...’ before ‘&’ token, ‘bool operator==(int)’ must have an argument of class or enumerated type."
school.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;
}
school.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 名称的形式定义。
void School () is a free function.
School::School(){ is the definition of the constructor.
这同样适用于您的其他成员。不是你的免费运营商。