数百个 "has no member named..." 错误 C++
Hundreds of "has no member named..." Errors C++
我刚刚开始 C++ 编程,如果这是一个可怕的菜鸟错误,请原谅,但它让我头疼。
我有多个正确编程的 .cpp 文件,编译后没有错误,所有文件都有自己的 .h 文件。
出于某种原因,当我尝试编译一个名为 "flyingUnit.cpp" 的 class 时,我遇到了数百个错误,大致如下:
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)
^
我完全不知道为什么会这样,从未找到 class 中使用的所有内容,完全目瞪口呆,而它似乎与所有其他 class 一起工作得很好es。为什么我会收到这些错误?
flyingUnit.h:
#ifndef FLYING_UNIT_H
#define FLYING_UNIT_H
#include "rider.h"
#include "dragon.h"
#include "fall.h"
const int MAX_FALLS = 20;
class FlyingUnit
{
public:
FlyingUnit( ) = default;
FlyingUnit( const char* dragonName, const char* dragonColour,
double length, double wingspan, bool breatheFire,
const char* riderName, const char* rank, int age,
const char* weyr );
void addFlyingUnit( const char* dragonName, const char* dragonColour,
double length, double wingspan, bool breatheFire,
const char* riderName, const char* rank, int age,
const char* weyr );
void setRank( const char* rank );
void setWeyr( const char* weyr );
int getFallCount( ) const { return fallCount; }
void addFall( const char* location, int day, int month, int year );
friend ostream& operator << (ostream& os, const FlyingUnit& f );
friend istream& operator >> (istream& is, FlyingUnit & f );
friend ofstream& operator << (ofstream& os, const FlyingUnit& f );
friend ifstream& operator >> (ifstream& is, FlyingUnit& f );
const char* getRiderName( ) const;
const char* getDragonName( ) const;
private:
Rider rider;
Dragon dragon;
Fall falls[ MAX_FALLS ];
int fallCount = 0;
};
#endif
flyingUnit.cpp:
#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;
}
编辑
我将提供另一个 class 和我制作的 .h 文件,它工作正常:
Rider.cpp
#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;
}
Rider.h
#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
您的 class 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)
您尝试初始化不存在的变量;长度、臂展和年龄...尝试在 header ..
中声明这些
我刚刚开始 C++ 编程,如果这是一个可怕的菜鸟错误,请原谅,但它让我头疼。
我有多个正确编程的 .cpp 文件,编译后没有错误,所有文件都有自己的 .h 文件。
出于某种原因,当我尝试编译一个名为 "flyingUnit.cpp" 的 class 时,我遇到了数百个错误,大致如下:
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)
^
我完全不知道为什么会这样,从未找到 class 中使用的所有内容,完全目瞪口呆,而它似乎与所有其他 class 一起工作得很好es。为什么我会收到这些错误?
flyingUnit.h:
#ifndef FLYING_UNIT_H
#define FLYING_UNIT_H
#include "rider.h"
#include "dragon.h"
#include "fall.h"
const int MAX_FALLS = 20;
class FlyingUnit
{
public:
FlyingUnit( ) = default;
FlyingUnit( const char* dragonName, const char* dragonColour,
double length, double wingspan, bool breatheFire,
const char* riderName, const char* rank, int age,
const char* weyr );
void addFlyingUnit( const char* dragonName, const char* dragonColour,
double length, double wingspan, bool breatheFire,
const char* riderName, const char* rank, int age,
const char* weyr );
void setRank( const char* rank );
void setWeyr( const char* weyr );
int getFallCount( ) const { return fallCount; }
void addFall( const char* location, int day, int month, int year );
friend ostream& operator << (ostream& os, const FlyingUnit& f );
friend istream& operator >> (istream& is, FlyingUnit & f );
friend ofstream& operator << (ofstream& os, const FlyingUnit& f );
friend ifstream& operator >> (ifstream& is, FlyingUnit& f );
const char* getRiderName( ) const;
const char* getDragonName( ) const;
private:
Rider rider;
Dragon dragon;
Fall falls[ MAX_FALLS ];
int fallCount = 0;
};
#endif
flyingUnit.cpp:
#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;
}
编辑
我将提供另一个 class 和我制作的 .h 文件,它工作正常:
Rider.cpp
#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;
}
Rider.h
#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
您的 class 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)
您尝试初始化不存在的变量;长度、臂展和年龄...尝试在 header ..
中声明这些