2017-03-17 97 views
-1

我正在努力解决错误,我一直无法找到解决方案。我似乎无法理解为什么“位置”不是作为一个班级找到的。这里是我的头文件:不是类或名称空间错误

#ifndef CLASS2_HPP 
#define CLASS2_HPP 

class Passenger 
{ 

public: 

    enum class Location 
    { 
     Business, 
     Economy, 
     Vip 
    }; 

    Passenger(Location clas_s, char* firstName, char* secondName, int seat, int terminal, float time_of_departure); 

    const char* get_location() const; 
    int get_seat() const; 
    int get_terminal() const; 
    float get_time() const; 
    char* get_firstName() const; 
    char* get_secondName() const; 
    void print() const; 

private: 

    Location _clas_s; 
    char _firstName; 
    char _secondName; 
    int _seat; 
    int _terminal; 
    float _time_of_departure; 

}; 


#endif // CLASS2 

这里是我的CPP文件:

#include <iostream> 
#include "Class2.hpp" 

using namespace std; 


Passenger::Passenger(Location clas_s, char* firstName, char* secondName, int seat, int terminal, float time_of_departure) 
: _clas_s(clas_s), _firstName(firstName), _secondName(secondName), _seat(seat), _terminal(terminal), _time_of_departure(time_of_departure) {}; 

void Passenger::print() const 
{ 
    cout << "Your name is " << _firstName 
    << " " << _secondName << endl 
    << "Your class is " << get_location() << endl 
    << "Your seat is " << _seat << endl 
    << "Your terminal is " << _terminal << endl 
    << "Your time of departure is " << _time_of_departure << endl; 
} 

const char* Passenger::get_location() const 
{ 
    switch (_clas_s) 
    { 
     case Location::Business : return "Business"; 
     case Location::Economy : return "Economy"; 
     case Location::Vip : return "Vip"; 
    } 
} 

int main() { 

    Passenger p((Passenger::Location::Vip), 'John', 'Johnson', 25, 2, 13.53); 
    p.print(); 

    return 0; 
} 

在此先感谢。

+0

就在我头上:您可能想要使用Passenger :: Location – Alain

+0

您在哪一行发生错误? – aschepler

+0

行是24/25/26/32 –

回答

0

看来你对C++ 03,所以要

enum Location 
{ 
    Business, 
    Economy, 
    Vip 
}; 

而且

case Business : return "Business"; 
case Economy : return "Economy"; 
case Vip : return "Vip"; 

编辑:当您创建我错

You forgot to set Passenger::Passenger(Passenger:: Location clas_s,...

+0

我做了你说的,但错误仍然存​​在 –

+0

@RostislavGeorgiev并使'char * _firstName; char * _secondName;' – em2er

+0

它仍然不起作用,但是我要继续努力修复它 –

0

Passenger你必须使用双引号传递字符串,而不是单q uotes(这是唯一的单字符):

Passenger p((Passenger::Location::Vip), "John", "Johnson", 25, 2, 13.53); 

然后,你已经宣布_firstName_secondNamechar。但是这只会让你存储一个字符!一个简单的解决方法是使用一个数组(但它会好得多使用适当的结构,这是一个std::string,使您不必担心大小):

char _firstName[50]; 
char _secondName[50]; 

然后,初始化都需要#include <cstring>并在constrcutor的主体(未在初始化列表中!)

strcpy(_firstName, firstName); 
strcpy(_secondName, secondName); 

(同样,std::string会更好,你可以将其设置在初始化列表中)

随着它编译的这些更改如预期的那样,输出为:

Your name is John Johnson
Your class is Vip
Your seat is 25
Your terminal is 2
Your time of departure is 13.53

测试ideone

0

您是否收到此错误:

Class.cpp:24:14: warning: use of enumeration in a nested name specifier is a C++11 extension [-Wc++11-extensions] 
     case Location::Business : return "Business"; 

正如它指出,它是一个C++ 11的功能,所以尽量用 “-std = C++ 11” 选项编译代码。或者用整数值改变开关盒:

switch (_clas_s) 
    { 
     case 0: return "Business"; 
     case 1: return "Economy"; 
     case 2: return "Vip"; 
    } 

希望有帮助。

相关问题