【发布时间】:2011-06-18 05:46:39
【问题描述】:
我不知道它有什么问题。我找不到错误在哪里,注释掉实现也不能解决错误。
头文件
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include <cstdlib> // Provides size_t
namespace main_savitch_3
{
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
// CONSTRUCTOR
sequence( );
// MODIFICATION MEMBER FUNCTIONS
void start( );
void advance( );
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current( );
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const;
bool is_item( ) const;
value_type current( ) const;
private:
value_type data[CAPACITY];
size_type used;
size_type current_index;
};
}
#endif
来源
#include "sequence1.h"
#include <assert.h>
namespace main_savitch_3
{
// Default constructer - sequence is empty
sequence::sequence()
{
used = current_index = 0;
}
// Start the iteration
void sequence::start()
{
current_index = 0;
}
// Iterate
void sequence::advance()
{
current_index++;
}
// Number of items in the sequence
sequence::size_type sequence::size() const
{
return used;
}
// Checks if there is a current item
bool sequence::is_item() const
{
return current_index <= used && used > 0;
}
// Returns the current value
sequence::value_type sequence::current() const
{
assert(is_item()); // no current item
return data[current_index];
}
// Adds an item BEFORE the current index
void sequence::insert(const value_type& entry)
{
assert(entry != 0); // pointer is invalid
assert(current_index < sequence::CAPACITY); // no room to add an item
// move items up - starting with the last item and working down to the current item
// arrays start at 0, so the -1 adjusts it
for (size_type i = used - 1; i >= current_index; i--)
data[i + 1] = data[i];
data[current_index] = entry;
}
// Adds an item AFTER the current index
void sequence::attach(const value_type& entry)
{
assert(entry != 0); // pointer is invalid
assert(current_index < sequence::CAPACITY); // no room to add an item
// move items up - starting with the last item and working down to the current item
// arrays start at 0, so the -1 adjusts it
for (size_type i = used - 1; i > current_index; i--)
data[i + 1] = data[i];
if (current_index = 0)
data[used] = entry;
else
data[current_index + 1] = entry;
}
// Removes the current item
void sequence::remove_current()
{
for (size_type i = current_index; i < used; i++)
data[i] = data[i + 1];
}
}
【问题讨论】:
-
这是一个链接错误。看起来您正在尝试构建没有
main()函数的可执行文件?您应该构建一个库,或者您需要一个包含main()的源文件。 -
Nitpick:如果你想要
size_t,你应该使用<cstddef>而不是<cstdlib>。 -
@Billy:
size_t在两个标头中都有定义。 -
@James:是的,但是
<cstddef>是“更小” :) -
我有类似的问题导致轻微的无知,没有选择 x64 构建,而包含的 lib 是 x64。