数以百计的成员没有名字......"错误C ++

时间:2016-09-02 16:55:00

标签: c++ file compilation header members

我刚刚开始使用C ++编程,如果这是一个可怕的noob错误,请原谅我,但它正在努力。

我有多个正确编程的.cpp文件,编译后没有错误,都有自己的.h文件。

出于某种原因,当我尝试编译一个名为" flyingUnit.cpp"我得到了数以百计的错误:

DECLARE @table TABLE( MinVal INT,MaxVal INT)
 INSERT INTO @table (MinVal,MaxVal)
VALUES  (0,10),(10,20),(20,30),(30,40),(40,50)

WITH cte_1
as
  (SELECT
    Nodes.Caption, Max(CPULoad.MaxLoad) as CPUMax,
    AVG(CPULoad.AvgLoad) as CPUAvg,
    Round(Max(CPULoad.MaxMemoryUsed / CPULoad.TotalMemory * 100),2) as MemMax, 
    Round(AVG(CPULoad.AvgPercentMemoryUsed),2) as MEMAvg

    FROM            
    Nodes INNER JOIN
          CPULoad ON Nodes.NodeID = CPULoad.NodeID

    WHERE
    Datetime >= DATEADD(MONTH,datediff(MONTH,0,getdate())-1,0)        AND 
    Datetime < DATEADD(MONTH,datediff(MONTH,0,getdate()),0) 

    GROUP BY Nodes.Caption)
    SELECT *, ISNULL('>= '+CAST( t.MinVal as VARCHAR(50))+' < '+CAST( t.MaxVal as VARCHAR(50)),'') CPUMaxInterval
            , ISNULL('>= '+CAST( t1.MinVal as VARCHAR(50))+' < '+CAST( t1.MaxVal as VARCHAR(50)),'') CPUAvgInterval
            , ISNULL('>= '+CAST( t2.MinVal as VARCHAR(50))+' < '+CAST( t2.MaxVal as VARCHAR(50)),'') MemMaxInterval
            , ISNULL('>= '+CAST( t3.MinVal as VARCHAR(50))+' < '+CAST( t3.MaxVal as VARCHAR(50)),'') MEMAvgInterval
    FROM cte_1 ct
     LEFT JOIN @table t on ct.CPUMax >= t.MinVal and ct.CPUMax <t.MaxVal
      LEFT JOIN @table t1 on ct.CPUAvg >= t.MinVal and ct.CPUAvg <t.MaxVal
       LEFT JOIN @table t2 on ct.MemMax >= t.MinVal and ct.MemMax <t.MaxVal
        LEFT JOIN @table t3 on ct.MEMAvg >= t.MinVal and ct.MEMAvg <t.MaxVal

我完全不知道为什么会发生这种情况,所有在课堂上使用的东西都找不到,完全傻眼,当它似乎与所有其他课程完美搭配时。为什么我会收到这些错误?

flyingUnit.h:

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.cpp:

#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

修改

我将提供另一个类和.h文件我已经制作并且工作正常:

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

Rider.h

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

1 个答案:

答案 0 :(得分:1)

您在班级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)
    

您尝试初始化不存在的变量;长度,翼展和年龄...尝试在标题中声明那些..