数以百计的;没有名为“的成员”;错误C++; 我刚开始C++编程,所以请原谅我这是一个可怕的错误,但它正在我的脑袋里。< /P>
我有多个正确编程的.cpp文件,编译后没有错误,都有自己的.h文件 出于某种原因,当我试图编译一个名为“flyingUnit.cpp”的类时,我会遇到数百个错误,都是这样的:数以百计的;没有名为“的成员”;错误C++; 我刚开始C++编程,所以请原谅我这是一个可怕的错误,但它正在我的脑袋里。< /P>,c++,file,compilation,header,members,C++,File,Compilation,Header,Members,我有多个正确编程的.cpp文件,编译后没有错误,都有自己的.h文件 出于某种原因,当我试图编译一个名为“flyingUnit.cpp”的类时,我会遇到数百个错误,都是这样的: flyingUnit.cpp: In constructor ‘FlyingUnit::FlyingUnit(const char*, const char*, double, double, bool, const char*, const char*, int, const char*)’: flyingUnit.cp
flyingUnit.cpp: In constructor ‘FlyingUnit::FlyingUnit(const char*, const char*, double, double,
bool, const char*, const char*, int, const char*)’:
flyingUnit.cpp:26:38: error: class ‘FlyingUnit’ does not have any field named ‘length’
const char* weyr ) : length(length), wingspan(wingspan), age(age)
^
我完全不知道为什么会发生这种情况,课堂上使用的所有东西都找不到,完全目瞪口呆,而其他所有的课程似乎都很好。为什么我会犯这些错误
flyingUnit.h:
\ifndef飞行单元\u H
#定义飞行单位
#包括“rider.h”
#包括“dragon.h”
#包括“fall.h”
const int MAX_FALLS=20;
类FlyingUnit
{
公众:
FlyingUnit()=默认值;
FlyingUnit(常量字符*龙名称,常量字符*龙颜色,
双倍长度,双倍翼展,深呼吸,
常量字符*riderName,常量字符*秩,整数年龄,
const char*weyr);
void addFlyingUnit(常量字符*龙名称,常量字符*龙颜色,
双倍长度,双倍翼展,深呼吸,
常量字符*riderName,常量字符*秩,整数年龄,
const char*weyr);
无效设置秩(常量字符*秩);
void setWeyr(const char*weyr);
int getFallCount()常量{return fallCount;}
void addFall(常量字符*位置、整数天、整数月、整数年);
friend ostream&operator>(istream&is、FlyingUnit&f);
流和操作员之友>(ifstream&is、FlyingUnit&f);
常量char*getRiderName()常量;
常量char*getDragonName()常量;
私人:
骑手;
龙;
瀑布[最大瀑布];
int fallCount=0;
};
#恩迪夫
flyingUnit.cpp:
#包括“flyingUnit.h”
#包括
#包括
#包括
int main()
{
返回0;
}
FlyingUnit::FlyingUnit(常量字符*龙名称,常量字符*龙颜色,
双倍长度,双倍翼展,深呼吸,
常量字符*riderName,常量字符*秩,整数年龄,
const char*weyr:长度(长度)、翼展(翼展)、年龄(年龄)
{
strcpy(此->龙名,龙名);
strcpy(本->龙彩,龙彩);
此->呼吸火焰=呼吸火焰;
strcpy(this->riderName,riderName);
strcpy(这个->等级,等级);
strcpy(这个->weyr,weyr);
}
ostream&operator您的类FlyingUnit中缺少一个成员变量“double length”
- 请注意,在您的合同中
FlyingUnit::FlyingUnit( const char* dragonName, const char* dragonColour,
double length, double wingspan, bool breatheFire,
const char* riderName, const char* rank, int age,
const char* weyr ) : length(length), wingspan(wingspan), age(age)
您尝试初始化不存在的变量;长度、翼展和年龄。。。尝试在标题中声明这些内容 你读到错误了吗class'FlyingUnit'没有任何名为'length'的字段。
对我来说非常清楚。我无法提供完整的错误,大约400行长,并表示“class FlyingUnit'没有名为'xxxxx'的成员”everything@Burgtaro如果您已经开发了此代码,请更频繁地编译,或者使用带有intellisense的IDE。所有这些都是FlyingUnit
s成员,而不是FlyingUnit
的成员。我修复了它,感谢您的帮助!我提供的错误就是一个例子,它发生在构造函数中的每个变量上,查看您在类中定义的数据成员。您试图访问的变量并不存在。@Burgtaro他给出的答案只是一个例子:P,而且大多数其他成员都只是丢失了是的,所有长度、翼展、年龄都丢失了。我对其他4个类和.h文件做了完全相同的操作,效果非常好,出于某种原因,这一个无法识别任何内容
#include "flyingUnit.h"
#include <iostream>
#include <cctype>
#include <string.h>
int main()
{
return 0;
}
FlyingUnit::FlyingUnit( const char* dragonName, const char* dragonColour,
double length, double wingspan, bool breatheFire,
const char* riderName, const char* rank, int age,
const char* weyr ) : length(length), wingspan(wingspan), age(age)
{
strcpy(this->dragonName, dragonName);
strcpy(this->dragonColour, dragonColour);
this->breatheFire = breatheFire;
strcpy(this->riderName, riderName);
strcpy(this->rank, rank);
strcpy(this->weyr, weyr);
}
ostream& operator << (ostream& os, const FlyingUnit& f)
{
os << "Dragon Name: " << f.dragonName
<< "Dragon Colour: " << f.dragonColour
<< "Length: " << f.length
<< "Wingspan: " << f.wingspan
<< "Breathes Fire: " << f.breatheFire
<< "Rider Name: " << f.riderName
<< "Rank: " << f.rank
<< "Age: " << f.age
<< "Weyr: " << f.weyr << endl;
return os;
}
istream& operator >> (istream& is, FlyingUnit & f)
{
char temp[500];
cout << "Enter Dragon Name: ";
is.getline(temp, 500);
strcpy(f.dragonName, temp);
cout << "Enter Dragon Colour: ";
is.getline(temp, 500);
strcpy(f.dragonColour);
cout << "Length: ";
is >> f.length;
is.ignore(100000, "\n");
cout << "Wingspan: ";
is >> f.wingspan;
is.ignore(100000, "\n");
//cout << "Breathes Fire: ";
// is.getline(temp, 500);
cout << "Enter Rider Name: ";
is.getline(temp, 500);
strcpy(f.riderName, temp);
cout << "Rank: ";
is >> f.rank;
is.ignore(100000, "\n");
cout << "Age: ";
is >> f.age;
is.ignore(100000, "\n");
cout << "Weyr: ";
is.getline(temp, 500);
strcpy(f.weyr, temp);
return is;
}
ofstream& operator << (ofstream& os, const FlyingUnit& f )
{
os << f.dragonName << endl
<< f.dragonColour << endl
<< f.length << endl
<< f.wingspan << endl
<< f.breatheFire << endl
<< f.riderName << endl
<< f.rank << endl
<< f.age << endl
<< f.weyr << endl;
return os;
}
ifstream& operator >> ( ifstream& is, FlyingUnit& f )
{
char temp[500];
is.getline(temp, 500);
strcpy(f.dragonName, temp);
is.getline(temp, 500);
strcpy(f.dragonColour);
is >> f.length;
is.ignore(100000, "\n");
is >> f.wingspan;
is.ignore(100000, "\n");
//cout << "Breathes Fire: ";
// is.getline(temp, 500);
is.getline(temp, 500);
strcpy(f.riderName, temp);
is >> f.rank;
is.ignore(100000, "\n");
is >> f.age;
is.ignore(100000, "\n");
is.getline(temp, 500);
strcpy(f.weyr, temp);
return is;
}
#include "rider.h"
int main()
{
return 0;
}
Rider::Rider( const char* name, int age, const char* rank,
const char* weyr ) : age( age )
{
this->name = new char[ strlen(name) + 1 ];
strcpy( this->name, name );
this->rank = new char[ strlen( rank ) + 1 ];
strcpy( this->rank, rank );
this->weyr = new char[ strlen( weyr ) + 1 ];
strcpy( this->weyr, weyr );
}
ostream& operator << ( ostream& os, const Rider& r)
{
os << "Rider name: " << r.name
<< " Age: " << r.age
<< " Rank: " << r.rank
<< " Weyr: " << r.weyr << endl;
return os;
}
istream& operator >> ( istream& is, Rider& r )
{
char temp[ 500 ];
cout << "Enter Rider name >> ";
is.getline( temp, 500 );
r.name = new char[strlen( temp ) + 1 ];
strcpy( r.name, temp );
cout << "Enter Rider age >> ";
is >> r.age;
// clean up the newline left after the int read
// as we are going to read text next.
is.ignore( 100000, '\n' );
cout << "Enter Rider rank >> " ;
is.getline( temp, 500 );
r.rank = new char[ strlen( temp ) + 1];
strcpy( r.rank, temp );
cout << "Enter Rider weyr >> " ;
is.getline( temp, 500 );
r.weyr = new char[ strlen( temp ) +1];
strcpy( r.weyr, temp );
return is;
}
ofstream& operator << ( ofstream& os, const Rider& r )
{
os << r.name << endl
<< r.age << endl
<< r.rank << endl
<< r.weyr << endl;
return os;
}
ifstream& operator >> ( ifstream& is, Rider & r )
{
char temp[ 500 ];
is.getline( temp, 500 );
r.name = new char[ strlen( temp ) + 1 ];
strcpy( r.name, temp );
is >> r.age;
is.ignore( 100000, '\n' );
is.getline(temp, 500 );
r.rank = new char[ strlen( temp ) + 1];
strcpy( r.rank, temp );
is.getline(temp, 500 );
r.weyr = new char[ strlen( temp ) + 1 ];
strcpy( r.weyr, temp );
return is;
}
#ifndef RIDER_H
#define RIDER_H
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::boolalpha;
#include <cstring>
using std::strcpy;
using std::strlen;
#include <fstream>
using std::ostream;
using std::istream;
using std::ofstream;
using std::ifstream;
class Rider
{
public:
Rider( ) = default;
Rider( const char* name, int age, const char* rank, const char* weyr );
void setName( const char* name );
void setRank( const char * rank );
void setWeyr( const char * weyr );
void setAge( int age ); // required when we enter a new Rider
// from the keyboard
const char* getName( ) const { return name; }
const char* getRank( ) const { return rank; }
const char* getWeyr( ) const { return weyr; }
int getAge( ) const { return age; }
friend ostream& operator << ( ostream& os, const Rider& r);
friend istream& operator >> ( istream& is, Rider& r );
friend ofstream& operator << ( ofstream& os, const Rider& r );
friend ifstream& operator >> ( ifstream& is, Rider & r );
private:
char * name = nullptr;
char * rank = nullptr;
char * weyr = nullptr;
int age = 0;
};
#endif
FlyingUnit::FlyingUnit( const char* dragonName, const char* dragonColour,
double length, double wingspan, bool breatheFire,
const char* riderName, const char* rank, int age,
const char* weyr ) : length(length), wingspan(wingspan), age(age)