Burgtaro Burgtaro - 3 months ago 15
C++ Question

Hundreds of "has no member named..." Errors C++

I've just started C++ programming so excuse me if this is a horrid noob mistake, but it's doing my head in.

I have multiple .cpp files correctly programmed and no errors after compiling them, all with their own .h files.

For some reason, when I try to compile one class named "flyingUnit.cpp" I get hundreds of errors, all along the lines of:

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)
^


I have absolutely no idea as to why this is happening, everything used in the class is never found, left completely dumbfounded, when it seems to work perfectly fine with all the other classes. Why am I getting these errors?

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


EDIT



I will provide another class and .h file I've made and it works fine:

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

Answer

you are missing a member variable "double length" in your class FlyingUnit

  • please note that in your contructor

    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)
    

you try to initialize variables that don't exist; length, wingspan, and age ... try to declare those in your header ..