模板问题 [英] Problems With Templates

查看:60
本文介绍了模板问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个学校项目,我必须尽快完成。


我必须实施B + Tree for这个项目。


我有TreeNode模板类。这将用于在树中保存键

索引。

我有从TreeNode派生的LeafNode模板类。这将用于

持有键和值的叶子。


一切似乎都很好,但它不能在MS.Net和gcc下编译。它是
编译Dev-C ++但链接器找不到插入函数并给出错误。


我检查过很多资源但是我找不到错误。如果你能解释我的错误,我会很高兴。


我得到的错误

TreeNode.h(5) :错误C2059:语法错误:''<''(与

模板相同的行)

TreeNode.h(5):错误C2143:语法错误:缺失''{''之前''<''(与模板相同的

行)

TreeNode.c(3):错误C2059:语法错误:'' <''


main.c(9):错误C2059:语法错误:''键''


,列表去了像这样。


如果你能解释我的错误,我会很高兴的。

////////////// TreeNode.h ////////////////

////////////// TreeNode.h ////// //////////

////////////// TreeNode.h /////////////// /

#ifndef __TREE_NODE__


#define __TREE_NODE__


模板< class KeyType,类ValType,int SIZE> ;


类TreeNode


{


pub lic:


TreeNode():nCount(0)

{};


虚拟void Insert(KeyType Key,ValType Val,KeyType& ParentKey,

TreeNode< KeyType,ValType,SIZE> *& LeftChild,


TreeNode< KeyType,ValType,SIZE> *& RightChild);


受保护:


int nCount;


KeyType e_Keys [ SIZE];


TreeNode< KeyType,ValType,SIZE> * e_Childs [SIZE];


};


#endif

//////// ////// LeafNode.h ////////////////

////////////// LeafNode.h ////////////////

////////////// LeafNode.h ///////// ///////


#include" TreeNode.h"


#ifndef __LEAF_NODE__


#define __LEAF_NODE__


模板< class KeyType,类ValType,int SIZE>


类LeafNode:public TreeNode< KeyType ,ValType,SIZE>


{


public:


LeafNode()


{


nCount = 0;


};


virtual void Insert(KeyType Key,ValType Val,KeyType& ParentKey,

TreeNode< KeyType,ValType,SIZE> *& LeftChild,


TreeNode< KeyType,ValType,SIZE> *& RightChild);


受保护:


ValType e_Vals [SIZE];


};


#endif


////////// Main.c for testing //////////////////


////////// Main.c for testing //////////////////

////////// Main.c进行测试//////////////////


int main(int argc,char * argv [])


{


int a,b,c,d;


LeafNode< int,int,7> TestNode;


TreeNode< int,int,7> * e,* f;


TestNode.Insert(10,10,a,e,f);


返回0;


}


Tayfun Ozdemir

Hello there,

I have a school project and I have to finish it soon.

I have to implement B+ Tree for the project.

I have TreeNode template class. That will be used to hold keys in the tree
index.
I have LeafNode template class derivered from TreeNode. That will be used to
hold keys and values in leaf.

Everything seems fine but it does not compile under MS.Net and gcc. It
compiles Dev-C++ but linker can not find Insert function and gives error.

I have checked many resources but I could not find the error. I would be
glad if you could explain me what is wrong.

Errors I get
TreeNode.h(5): error C2059: syntax error : ''<'' (The same line with
template)
TreeNode.h(5): error C2143: syntax error : missing ''{'' before ''<'' (The same
line with template)
TreeNode.c(3): error C2059: syntax error : ''<''

main.c(9): error C2059: syntax error : ''type''

and the list goes like this.

I would be glad if you could explain me what is wrong.
////////////// TreeNode.h////////////////
////////////// TreeNode.h////////////////
////////////// TreeNode.h////////////////
#ifndef __TREE_NODE__

#define __TREE_NODE__

template <class KeyType, class ValType, int SIZE>

class TreeNode

{

public:

TreeNode() : nCount(0)

{};

virtual void Insert(KeyType Key, ValType Val, KeyType &ParentKey,

TreeNode<KeyType, ValType, SIZE> *&LeftChild,

TreeNode<KeyType, ValType, SIZE> *&RightChild);

protected:

int nCount;

KeyType e_Keys[SIZE];

TreeNode<KeyType, ValType, SIZE> *e_Childs[SIZE];

};

#endif
////////////// LeafNode.h////////////////
////////////// LeafNode.h////////////////
////////////// LeafNode.h////////////////

#include "TreeNode.h"

#ifndef __LEAF_NODE__

#define __LEAF_NODE__

template <class KeyType, class ValType, int SIZE>

class LeafNode : public TreeNode<KeyType, ValType, SIZE>

{

public:

LeafNode()

{

nCount = 0;

};

virtual void Insert(KeyType Key, ValType Val, KeyType &ParentKey,

TreeNode<KeyType, ValType, SIZE> *&LeftChild,

TreeNode<KeyType, ValType, SIZE> *&RightChild);

protected:

ValType e_Vals[SIZE];

};

#endif

////////// Main.c for testing //////////////////

////////// Main.c for testing //////////////////

////////// Main.c for testing //////////////////

int main(int argc, char *argv[])

{

int a, b, c, d;

LeafNode<int, int, 7> TestNode;

TreeNode<int, int, 7> *e, *f;

TestNode.Insert(10, 10, a, e, f);

return 0;

}

Tayfun Ozdemir

推荐答案

我可以发给你我的所有程序如果有帮助的话。

真诚。

Tayfun?zdemir。

Tayfun?zdemir < NO **** @ NOSPAM.COM> ;, haber iletisinde tunlary

yazdy:8M ****************** @ fe08.news。 easynews.com。 ..
I can send you all of my program if that helps.
Sincerely.
Tayfun ?zdemir.
"Tayfun ?zdemir" <NO****@NOSPAM.COM>, haber iletisinde tunlary
yazdy:8M******************@fe08.news.easynews.com. ..
你好,

我有一个学校项目,我很快就要完成它。

我必须实现B + Tree项目。

我有TreeNode模板类。这将用于在树中保存键
索引。
我有从TreeNode派生的LeafNode模板类。这将用于保存键中的键和值。

一切似乎都很好,但它不能在MS.Net和gcc下编译。它编译Dev-C ++但是链接器找不到插入函数并给出错误。

我检查了很多资源但是我找不到错误。如果你能解释我的错误,我会很高兴。

我得到的错误
TreeNode.h(5):错误C2059:语法错误:''<' '(与
模板相同的行)
TreeNode.h(5):错误C2143:语法错误:缺少''''''''<'''(
同一行使用模板)
TreeNode.c(3):错误C2059:语法错误:''<''

main.c(9):错误C2059:语法错误:''类型''

这个清单是这样的。

如果你能解释我的错误,我会很高兴的。

//// ////////// TreeNode.h ////////////////
////////////// TreeNode.h ////////////////
////////////// TreeNode.h ///////////// ///
#ifndef __TREE_NODE__

#define __TREE_NODE__

模板<类KeyType,类ValType,int SIZE>

类TreeNode

公开:

TreeNode():nCount(0)

{};

虚拟空插入(KeyType K. ey,ValType Val,KeyType& ParentKey,

TreeNode< KeyType,ValType,SIZE> *& LeftChild,

TreeNode< KeyType,ValType,SIZE> *& RightChild);

受保护:

int nCount;

KeyType e_Keys [SIZE];

TreeNode< ; KeyType,ValType,SIZE> * e_Childs [SIZE];

};

#endif

////////////// LeafNode。 h ////////////////
////////////// LeafNode.h //////////// ////
////////////// LeafNode.h ////////////////

#包括TreeNode.h

#ifnf __LEAF_NODE__

#define __LEAF_NODE__

模板<类KeyType,类ValType,int SIZE> <类LeafNode:public TreeNode< KeyType,ValType,SIZE>

公开:

LeafNode()

{

nCount = 0;

};

虚拟空插入(KeyType Key,ValType Val,KeyType& ; ParentKey,

TreeNode< KeyType,ValType,SIZE> *& LeftChild,

TreeNode< KeyType,ValType,SIZE> *& RightChild);


受保护:

ValType e_Vals [SIZE];

};

#endif

////////// Main.c for testing //////////////////

/////// /// Main.c进行测试/////// ///////////

//////////主要用于测试//////////////// //

int main(int argc,char * argv [])

{/>
int a,b,c,d;

LeafNode< int,int,7> TestNode;

TreeNode< int,int,7> * e,* f;

TestNode.Insert(10,10,a,e,f);

返回0;

}


Tayfun Ozdemir
Hello there,

I have a school project and I have to finish it soon.

I have to implement B+ Tree for the project.

I have TreeNode template class. That will be used to hold keys in the tree
index.
I have LeafNode template class derivered from TreeNode. That will be used
to hold keys and values in leaf.

Everything seems fine but it does not compile under MS.Net and gcc. It
compiles Dev-C++ but linker can not find Insert function and gives error.

I have checked many resources but I could not find the error. I would be
glad if you could explain me what is wrong.

Errors I get
TreeNode.h(5): error C2059: syntax error : ''<'' (The same line with
template)
TreeNode.h(5): error C2143: syntax error : missing ''{'' before ''<'' (The
same line with template)
TreeNode.c(3): error C2059: syntax error : ''<''

main.c(9): error C2059: syntax error : ''type''

and the list goes like this.

I would be glad if you could explain me what is wrong.
////////////// TreeNode.h////////////////
////////////// TreeNode.h////////////////
////////////// TreeNode.h////////////////
#ifndef __TREE_NODE__

#define __TREE_NODE__

template <class KeyType, class ValType, int SIZE>

class TreeNode

{

public:

TreeNode() : nCount(0)

{};

virtual void Insert(KeyType Key, ValType Val, KeyType &ParentKey,

TreeNode<KeyType, ValType, SIZE> *&LeftChild,

TreeNode<KeyType, ValType, SIZE> *&RightChild);

protected:

int nCount;

KeyType e_Keys[SIZE];

TreeNode<KeyType, ValType, SIZE> *e_Childs[SIZE];

};

#endif
////////////// LeafNode.h////////////////
////////////// LeafNode.h////////////////
////////////// LeafNode.h////////////////

#include "TreeNode.h"

#ifndef __LEAF_NODE__

#define __LEAF_NODE__

template <class KeyType, class ValType, int SIZE>

class LeafNode : public TreeNode<KeyType, ValType, SIZE>

{

public:

LeafNode()

{

nCount = 0;

};

virtual void Insert(KeyType Key, ValType Val, KeyType &ParentKey,

TreeNode<KeyType, ValType, SIZE> *&LeftChild,

TreeNode<KeyType, ValType, SIZE> *&RightChild);

protected:

ValType e_Vals[SIZE];

};

#endif

////////// Main.c for testing //////////////////

////////// Main.c for testing //////////////////

////////// Main.c for testing //////////////////

int main(int argc, char *argv[])

{

int a, b, c, d;

LeafNode<int, int, 7> TestNode;

TreeNode<int, int, 7> *e, *f;

TestNode.Insert(10, 10, a, e, f);

return 0;

}

Tayfun Ozdemir



Tayfun?zdemir写道:
Tayfun ?zdemir wrote:
你好那里,

我有一个学校项目,我很快就要完成它。

我必须为项目实施B + Tree。

我拥有TreeNode模板类。这将用于在树中保存键
索引。
我有从TreeNode派生的LeafNode模板类。这将用于在叶子中保存键和值。

一切似乎都很好,但它不能在MS.Net和gcc下编译。它编译Dev-C ++但是链接器找不到插入函数并给出错误。

我检查了很多资源但是我找不到错误。如果你能解释我的错误,我会很高兴。

我得到的错误
TreeNode.h(5):错误C2059:语法错误:''<' '(与
模板相同的行)
TreeNode.h(5):错误C2143:语法错误:缺少''''''''<'''(相同的
行使用模板)
TreeNode.c(3):错误C2059:语法错误:''<''

main.c(9):错误C2059:语法错误:''类型''
Hello there,

I have a school project and I have to finish it soon.

I have to implement B+ Tree for the project.

I have TreeNode template class. That will be used to hold keys in the tree
index.
I have LeafNode template class derivered from TreeNode. That will be usedto
hold keys and values in leaf.

Everything seems fine but it does not compile under MS.Net and gcc. It
compiles Dev-C++ but linker can not find Insert function and gives error.

I have checked many resources but I could not find the error. I would be
glad if you could explain me what is wrong.

Errors I get
TreeNode.h(5): error C2059: syntax error : ''<'' (The same line with
template)
TreeNode.h(5): error C2143: syntax error : missing ''{'' before ''<'' (The same
line with template)
TreeNode.c(3): error C2059: syntax error : ''<''

main.c(9): error C2059: syntax error : ''type''




代码编译在这里很好,但我无法链接,因为有些文件是

缺失。


1).c文件通常被解释为C文件,因此您的系统可能会使用C编译器编译它们。制作.cpp文件。

2)如果他们的成员

函数没有内联定义(在类中),大多数当前编译器都无法编译模板。那可能是为什么

dev-c ++在链接时失败了。

Jonathan



Code compiles fine here, but I cannot link because some files are
missing.

1) .c files are usually interpreted as being C files so your system may
compile them with a C compiler. Make them .cpp files.
2) most current compilers cannot compile templates if their member
functions are not defined inline (in the class). That`s probably why
dev-c++ is failing at link-time.
Jonathan


谢谢Jonathan。你是对的。一些文件被命名为c。而不是

" cpp" :-)。


现在编译好了。但我得到链接器错误。如果你能够解释我的项目有什么问题,我会很高兴。我检查了函数

参数和返回值。他们都相互匹配。我找不到

错误。


////// TreeNode.h ///////

////// TreeNode.h ///////

////// TreeNode.h ///////


#ifndef __TREE_NODE__

#define __TREE_NODE__

模板< class KeyType,类ValType,int SIZE>

class TreeNode
{

public:

TreeNode():nCount(0)

{};

virtual void Insert(KeyType Key,ValType Val,KeyType& ParentKey,

TreeNode< KeyType,ValType,SIZE> *& LeftChild,

TreeNode< ; KeyType,ValType,SIZE> *& RightChild);

受保护:

int nCount;

KeyType e_Keys [SIZE];

TreeNode< KeyType,ValType,SIZE> * e_Childs [SIZE];

};


#endif


//// // TreeNode.cpp ///////

////// TreeNode.cpp ///////

////// TreeNode.cpp ///////


#include" TreeNode.h"

模板< class KeyType,类ValType ,int SIZE>

void TreeNode< KeyType,ValType,SIZE> :: Insert(KeyType Key,ValType Val,

KeyType& ParentKey,

TreeNode< KeyType,ValType,SIZE> *& LeftChild,

TreeNode< KeyType,ValType,SIZE> *& RightChild)

{

返回;

}

////// LeafNode.h ///////

//// // LeafNode.h ///////

////// LeafNode.h ///////


#include " TreeNode.h"

#ifndef __LEAF_NODE__

#define __LEAF_NODE__


template< class KeyType, class ValType,int SIZE>

class LeafNode:public TreeNode< KeyType,ValType,SIZE&g t;

{

public:


LeafNode()

{

nCount = 0;

};

虚拟空插入(KeyType Key,ValType Val,KeyType& ParentKey,

TreeNode< KeyType,ValType,SIZE> *& LeftChild,

TreeNode< KeyType,ValType,SIZE> *& RightChild);


受保护:

ValType e_Vals [SIZE];

};

#endif


////// LeafNode.cpp ///////

////// LeafNode.cpp ///////

////// LeafNode.cpp ///////


#include" LeafNode.h"


模板<类KeyType,类ValType,int SIZE>

void LeafNode< KeyType,ValType,SIZE> :: Insert(KeyType Key,ValType Val,

KeyType& ParentKey,

TreeNode< KeyType,ValType,SIZE> *& LeftChild,

TreeNode< KeyType ,ValType,SIZE> *& RightChild)

{

int i = nCount;


//在列出一个直到我们找到现场

while(i!= 0&& e_Keys [i - 1]> Key){

e_Keys [i] = e_Keys [i - 1];

e_Vals [i] = e_Vals [i - 1];

i - ;

}


//我们当场为列表添加新值

e_Keys [i] = Key;

e_Vals [i] = Value;

nCount ++;


//检查我们是否已满并已做好准备分割

if(nCount == SIZE){

LeafNode< KeyType,ValType,SIZE> * Right = new LeafNode< KeyType,

ValType,SIZE> ;;


for(i = nCount / 2; i< nCount; i ++){

右 - > e_Keys [i - nCount / 2] = e_Keys [i];

右 - > e_Vals [i - nCount / 2] = e_Vals [i] ;

右 - > nCount = i - nCount / 2 + 1;

}


}

返回;

}


////// Main.cpp ///////

////// Main.cpp ///////

////// Main.cpp ///////


#include< cstdio>


#include" LeafNode.h"

#include" TreeNode.h"


int main(int argc,char * argv [])

{

int a;

LeafNode< int,int,7> TestNode;

TreeNode< int,int,7> * e,* f;

TestNode.Insert(10,10,a,e,f);

返回0;

}


我真的很感谢你的帮助。对于一切都是好的。

此致。

Tayfun?zdemir。
Thank you so Jonathan. You were right. Some files were named "c" instead of
"cpp" :-).

It compiles fine now. But I get linker error. I would be very glad if you
can explain what is wrong with my project. I have checked function
parameters and return value. They all match each other. I could not find the
error.

////// TreeNode.h ///////
////// TreeNode.h ///////
////// TreeNode.h ///////

#ifndef __TREE_NODE__
#define __TREE_NODE__
template <class KeyType, class ValType, int SIZE>
class TreeNode
{
public:
TreeNode() : nCount(0)
{};

virtual void Insert(KeyType Key, ValType Val, KeyType &ParentKey,
TreeNode<KeyType, ValType, SIZE> *&LeftChild,
TreeNode<KeyType, ValType, SIZE> *&RightChild);
protected:
int nCount;
KeyType e_Keys[SIZE];
TreeNode<KeyType, ValType, SIZE> *e_Childs[SIZE];
};

#endif


////// TreeNode.cpp ///////
////// TreeNode.cpp ///////
////// TreeNode.cpp ///////

#include "TreeNode.h"

template <class KeyType, class ValType, int SIZE>
void TreeNode<KeyType, ValType, SIZE>::Insert(KeyType Key, ValType Val,
KeyType &ParentKey,
TreeNode<KeyType, ValType, SIZE> *&LeftChild,
TreeNode<KeyType, ValType, SIZE> *&RightChild)
{
return;
}
////// LeafNode.h ///////
////// LeafNode.h ///////
////// LeafNode.h ///////

#include "TreeNode.h"

#ifndef __LEAF_NODE__
#define __LEAF_NODE__

template <class KeyType, class ValType, int SIZE>
class LeafNode : public TreeNode<KeyType, ValType, SIZE>
{
public:

LeafNode()
{
nCount = 0;
};
virtual void Insert(KeyType Key, ValType Val, KeyType &ParentKey,
TreeNode<KeyType, ValType, SIZE> *&LeftChild,
TreeNode<KeyType, ValType, SIZE> *&RightChild);

protected:
ValType e_Vals[SIZE];
};
#endif


////// LeafNode.cpp ///////
////// LeafNode.cpp ///////
////// LeafNode.cpp ///////

#include "LeafNode.h"

template <class KeyType, class ValType, int SIZE>
void LeafNode<KeyType, ValType, SIZE>::Insert(KeyType Key, ValType Val,
KeyType &ParentKey,
TreeNode<KeyType, ValType, SIZE> *&LeftChild,
TreeNode<KeyType, ValType, SIZE> *&RightChild)
{
int i = nCount;

// Shift values in the list by one until we find the spot
while (i != 0 && e_Keys[i - 1] > Key) {
e_Keys[i] = e_Keys[i - 1];
e_Vals[i] = e_Vals[i - 1];
i--;
}

// We are on the spot add new value to the list
e_Keys[i] = Key;
e_Vals[i] = Value;
nCount++;

// Check if we are full and are ready to split
if (nCount == SIZE) {
LeafNode<KeyType, ValType, SIZE> *Right = new LeafNode<KeyType,
ValType, SIZE>;

for (i = nCount / 2; i < nCount; i++) {
Right->e_Keys[i - nCount / 2] = e_Keys[i];
Right->e_Vals[i - nCount / 2] = e_Vals[i];
Right->nCount = i - nCount / 2 + 1;
}

}
return;
}


////// Main.cpp ///////
////// Main.cpp ///////
////// Main.cpp ///////

#include <cstdio>

#include "LeafNode.h"
#include "TreeNode.h"

int main(int argc, char *argv[])
{
int a;
LeafNode<int, int, 7> TestNode;
TreeNode<int, int, 7> *e, *f;
TestNode.Insert(10, 10, a, e, f);
return 0;
}

I really appreciate your help. Thanx for everything.
Sincerely.
Tayfun ?zdemir.


这篇关于模板问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆