模板文件在 CodeLite 中不能正常工作?
Template file not working correctly in CodeLite?
每次我在工作场所创建新项目时,我都会 运行 遇到模板问题。例如,我将创建一个新的 class,CodeLite 将为我创建一个 .h 文件和一个 .cpp 文件,然后我将通过重命名该文件将该 .cpp 文件更改为 .template。它有时有效,有时无效。有时我必须清理我的工作场所才能工作,其他时候我需要退出 CodeLite 并重新打开它。这次这些解决方案对我不起作用,但也许我遗漏了一些东西。这是我的代码:
.h 文件
#ifndef TABLE1_H
#define TABLE1_H
#include <cstdlib> // Provides size_t
namespace main_savitch_12A
{
template <class RecordType>
class table
{
public:
// MEMBER CONSTANT -- See Appendix E if this fails to compile.
static const std::size_t CAPACITY = 811;
// CONSTRUCTOR
table( );
// MODIFICATION MEMBER FUNCTIONS
void insert(const RecordType& entry);
void remove(int key);
// CONSTANT MEMBER FUNCTIONS
bool is_present(int key) const;
void find(int key, bool& found, RecordType& result) const;
std::size_t size( ) const { return used; }
private:
// MEMBER CONSTANTS -- These are used in the key field of special records.
static const int NEVER_USED = -1;
static const int PREVIOUSLY_USED = -2;
// MEMBER VARIABLES
RecordType data[CAPACITY];
std::size_t used;
// HELPER FUNCTIONS
std::size_t hash(int key) const;
std::size_t next_index(std::size_t index) const;
void find_index(int key, bool& found, std::size_t& index) const;
bool never_used(std::size_t index) const;
bool is_vacant(std::size_t index) const;
};
}
#include "table1.template" // Include the implementation.
#endif
.模板文件
template<class RecordType>
table<RecordType>::table(){
used = 32;
}
主文件
#include <stdio.h>
#include "table1.h"
int main(int argc, char **argv)
{
printf("hello world\n");
return 0;
}
我的模板和 .h 文件名为 table1。当我 运行 程序在模板文件中时出现错误。它显示:"table does not name a type" 我该如何解决这个问题?
在您的模板实现中,您缺少命名空间,请使用:
template <class RecordType>
main_savitch_12A::table<RecordType>::table()
{
used = 32;
};
每次我在工作场所创建新项目时,我都会 运行 遇到模板问题。例如,我将创建一个新的 class,CodeLite 将为我创建一个 .h 文件和一个 .cpp 文件,然后我将通过重命名该文件将该 .cpp 文件更改为 .template。它有时有效,有时无效。有时我必须清理我的工作场所才能工作,其他时候我需要退出 CodeLite 并重新打开它。这次这些解决方案对我不起作用,但也许我遗漏了一些东西。这是我的代码:
.h 文件
#ifndef TABLE1_H
#define TABLE1_H
#include <cstdlib> // Provides size_t
namespace main_savitch_12A
{
template <class RecordType>
class table
{
public:
// MEMBER CONSTANT -- See Appendix E if this fails to compile.
static const std::size_t CAPACITY = 811;
// CONSTRUCTOR
table( );
// MODIFICATION MEMBER FUNCTIONS
void insert(const RecordType& entry);
void remove(int key);
// CONSTANT MEMBER FUNCTIONS
bool is_present(int key) const;
void find(int key, bool& found, RecordType& result) const;
std::size_t size( ) const { return used; }
private:
// MEMBER CONSTANTS -- These are used in the key field of special records.
static const int NEVER_USED = -1;
static const int PREVIOUSLY_USED = -2;
// MEMBER VARIABLES
RecordType data[CAPACITY];
std::size_t used;
// HELPER FUNCTIONS
std::size_t hash(int key) const;
std::size_t next_index(std::size_t index) const;
void find_index(int key, bool& found, std::size_t& index) const;
bool never_used(std::size_t index) const;
bool is_vacant(std::size_t index) const;
};
}
#include "table1.template" // Include the implementation.
#endif
.模板文件
template<class RecordType>
table<RecordType>::table(){
used = 32;
}
主文件
#include <stdio.h>
#include "table1.h"
int main(int argc, char **argv)
{
printf("hello world\n");
return 0;
}
我的模板和 .h 文件名为 table1。当我 运行 程序在模板文件中时出现错误。它显示:"table does not name a type" 我该如何解决这个问题?
在您的模板实现中,您缺少命名空间,请使用:
template <class RecordType>
main_savitch_12A::table<RecordType>::table()
{
used = 32;
};