【发布时间】:2016-09-02 16:54:59
【问题描述】:
我刚开始 C++ 编程,如果这是一个可怕的菜鸟错误,请原谅,但它让我很头疼。
我有多个正确编程的 .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.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 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;
}
编辑
我将提供我制作的另一个类和 .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;
}
骑士.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’ does not have any field named ‘length’对我来说很清楚。 -
我无法提供完整的错误,它大约 400 行长,并说“'Class FlyingUnit' 没有名为 'xxxxx' 的成员”
-
@Burgtaro 如果您已开发此代码,请更频繁地编译,或使用带智能感知的 IDE。
-
所有这些都是
FlyingUnits 成员的成员,而不是FlyingUnit的成员。 -
我修好了,谢谢你的帮助!
标签: c++ file compilation header members