排序列表,实现文件
Sorted List, implementation file
所以我正在解决一个问题,并且提供了信息,但无论出于何种原因,它都无法编译。我完全从教科书中复制,但在实现文件中出现错误,例如 const(非成员函数不允许使用类型限定符)和值(成员不可访问)。我猜这只是一个简单的错字或顶部包含的内容,但我无法弄清楚。
// SPECIFICATION FILE ( itemtype.h )
const int MAX_ITEM = 5 ;
enum RelationType { LESS, EQUAL, GREATER } ;
class ItemType // declares class data type
{
public : // 3 public member functions
RelationType ComparedTo ( ItemType ) const;
void Print() const;
void Initialize(int number);
private:
int value;
};
// IMPLEMENTATION FILE ( itemtype.cpp )
// Implementation depends on the data type of value.
#include “itemtype.h”
#include <iostream>
using namespace std;
RelationType ComparedTo ( ItemType otherItem ) const
{
if ( value < otherItem.value )
return LESS ;
else if ( value > otherItem.value )
return GREATER ;
else
return EQUAL ;
}
void Print ( ) const
{ cout << value << endl ;
}
void Initialize ( int number )
{
value = number ;
}
有两种可能:要么你的书有误,要么你没有完全复制代码。
当成员函数定义在class定义之外时,需要告诉编译器它们属于哪个class:
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
// ...
}
// ...
void ItemType::Print() const
{
// ...
}
等等。
(从 C++ 的角度来看,class、头文件和实现之间没有任何关系。)
所以我正在解决一个问题,并且提供了信息,但无论出于何种原因,它都无法编译。我完全从教科书中复制,但在实现文件中出现错误,例如 const(非成员函数不允许使用类型限定符)和值(成员不可访问)。我猜这只是一个简单的错字或顶部包含的内容,但我无法弄清楚。
// SPECIFICATION FILE ( itemtype.h )
const int MAX_ITEM = 5 ;
enum RelationType { LESS, EQUAL, GREATER } ;
class ItemType // declares class data type
{
public : // 3 public member functions
RelationType ComparedTo ( ItemType ) const;
void Print() const;
void Initialize(int number);
private:
int value;
};
// IMPLEMENTATION FILE ( itemtype.cpp )
// Implementation depends on the data type of value.
#include “itemtype.h”
#include <iostream>
using namespace std;
RelationType ComparedTo ( ItemType otherItem ) const
{
if ( value < otherItem.value )
return LESS ;
else if ( value > otherItem.value )
return GREATER ;
else
return EQUAL ;
}
void Print ( ) const
{ cout << value << endl ;
}
void Initialize ( int number )
{
value = number ;
}
有两种可能:要么你的书有误,要么你没有完全复制代码。
当成员函数定义在class定义之外时,需要告诉编译器它们属于哪个class:
RelationType ItemType::ComparedTo(ItemType otherItem) const
{
// ...
}
// ...
void ItemType::Print() const
{
// ...
}
等等。
(从 C++ 的角度来看,class、头文件和实现之间没有任何关系。)