typedef 和 类模板 的问题

这是我使用类模板定义的一个类 LNode

template
class LNode
{
public:
ElemType date;
LNode *next;
};

我想对 *LNode 进行重命名操作

typedef LNode *LinkList;

用int没有问题
但是我想对所有所有类型都重命名为LinkList,就会编译失败

template
typedef LNode *LinkList;//编译失败

请教
需要怎么修改

0

1个回答

 只能放在类里面

#include <iostream>

using namespace std;

template<typename ElemType>
class LNode
{
public:
    ElemType date;
    LNode *next;
};

template<typename ElemType>
class MakeType
{
typedef LNode<ElemType> *LinkList;
};

int main() {
    cout << "hello https://tool.lu/" << endl;
    return 0;
}

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
关于c++中类型定义typedef和类模板能否共用问题
#include #include using namespace std; template class A { typedef deque cont; protected: cont c; public: void push(T&t) { c.push_back(t); } void display() { for (int i = 0; i < c.size()
类模板问题
三个函数:rn1.Node.hrn#ifndef NODE_Hrn#define NODE_Hrnusing namespace std;rntemplaternclass Nodernrn public:rn T data;rn Node* next;rnrn Node()rn rn next=NULL;rn rnrn Node(T data,Node *next=NULL)rn rn this->data=data;rn this->next=next;rn rn;rn#endifrnrn2.singlyLinkedList.hrnrn#ifndef NODE_Hrn#define NODE_Hrn#include"Node.h"rn#includernusing namespace std;rntemplate class Node;rntemplaternclass singlyLinkedListrnrn public:rn Node* head;rn singlyLinkedList();rn singlyLinkedList(T value[],int n);rn ~singlyLinkedList();rn bool isEmpty();rn int length();rn Node* getNode(int i);rn T get(int i);rn bool set(int i,T x);rn friend ostream& operator<<(ostream& out,singlyLinkedList& list)rn rn Node* p=list.head;rn out<<"(";rn while(p!=NULL)rn rn out<data;rn p=p->next;rn if(p!=NULL)rn out<<",";rn rn out<<")";rn return out;rn rn Node* insert(int i,T x);rn bool remove(int i,T& old);rn void clear();rn void concat(singlyLinkedList &list);rnrn;rnrntemplaternsinglyLinkedList::singlyLinkedList()rnrn head=NULL;rnrnrntemplaternsinglyLinkedList::singlyLinkedList(T value[],int n)rnrn head=NULL;rn if(n>0)rn rn head=new Node(value[0]);rn Node* rear=head;rn int i=1;rn for(i=1;inext=new Node(value[i]);rn rear=rear->next;rn rnrn rnrnrntemplaternsinglyLinkedList::~singlyLinkedList()rnrn clear();rnrnrntemplaternbool singlyLinkedList::isEmpty()rnrn return head==NULL;rnrnrntemplaternint singlyLinkedList::length()rnrn int i=0;rn Node* p=head;rn while(p!=NULL)rn rn ++i;rn p=p->next;rn rn return i;rnrnrntemplaternNode* singlyLinkedList::getNode(int i)rnrn if(i<0)rn return NULL;rn int j=0;rn Node* p=head;rn while(p!=NULL&&j!=i)rn rn ++j;rn p=p->next;rn rn return p;rnrnrntemplaternT singlyLinkedList::get(int i)rnrn Node* p=getNode(i);rn if(p!=NULL)rn return p->data;rn throw "指定序号无效";rnrnrntemplaternbool singlyLinkedList::set(int i,T x)rnrn Node* p=getNode(i);rn if(p!=NULL)rn rn p->data=x;rn return true;rn rn return false;rnrnrntemplaternvoid singlyLinkedList::clear()rnrn Node* p=head;rn while(p!=NULL)rn rn Node* q=p;rn p=p->next;rn delete q;rn rn head=NULL;rnrnrntemplaternbool singlyLinkedList::remove(int i,T& old)rnrn if(i==0) rn rn Node* q=head;rn old=q->data;rn head=head->next;rn delete q;rn return true;rn rn Node* p=getNode(i-1);rn if(p!=NULL)rn rn Node* q=p->next;rn old=q->data;rn p->next=q->next;rn delete q;rn return true;rn rn return false;rnrnrntemplaternNode* singlyLinkedList::insert(int i,T x)rnrn Node* q=NULL;rn if(head=NULL||i<=0)rn rn q=new Node(x,head);rn head=q;rn rn elsern rn int j=0;rn Node* p=head;rn while(p->next!=NULL&&jnext;rn q=new Node(x,p->next);rn p->next=q;rn rn return q;rnrnrntemplaternvoid singlyLinkedList::concat(singlyLinkedList &list)rnrn if(this->head==NULL)rn this->head=list.head;rn elsern rn Node* p=head;rn while(p->next!=NULL)rn p=p->next;rn p->next=list.head;rn rn list.head=NULL;rnrn#endifrnrn3.main.cpprn#includern#include"singlyLinkedList.h"rnusing namespace std;rnint main()rnrn singlyLinkedList lista("abc",3);rn singlyLinkedList listb("xy",2);rn lista.concat(listb);rn cout<'rn c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(21) : while compiling class-template member function 'class std::basic_ostream > &__cdecl singlyLinkedList::ornperator <<(class std::basic_ostream > &,class singlyLinkedList &)'rnc:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(26) : error C2227: left of '->data' must point to class/struct/unionrn c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(21) : while compiling class-template member function 'class std::basic_ostream > &__cdecl singlyLinkedList::ornperator <<(class std::basic_ostream > &,class singlyLinkedList &)'rnc:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(27) : error C2027: use of undefined type 'Node'rn c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(21) : while compiling class-template member function 'class std::basic_ostream > &__cdecl singlyLinkedList::ornperator <<(class std::basic_ostream > &,class singlyLinkedList &)'rnc:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(27) : error C2227: left of '->next' must point to class/struct/unionrn c:\program files\microsoft visual studio\myprojects\ds_singlylinkedlist\singlylinkedlist.h(21) : while compiling class-template member function 'class std::basic_ostream > &__cdecl singlyLinkedList::ornperator <<(class std::basic_ostream > &,class singlyLinkedList &)'rnError executing cl.exe.rnCreating browse info file...rnrnDS_singlyLinkedList.exe - 4 error(s), 0 warning(s)rnrn有没有会的帮我看看
类模板的问题
类的成员函数能返回一个未知类型吗?rn比如下面这:rntemplate rn T node::display(int index)rnrn..........rnreturn XXXX;rnrnrn这个怎么编译不过去,该怎么办
类模板的问题?
// file llist.hrn#ifndef LinearList_rn#define LinearList_rn#include rn#include rn#include "xcept.h"rnrntemplaternclass LinearList rn public:rn LinearList(int MaxListSize = 10); // constructorrn ~LinearList() delete [] element; // destructorrn bool IsEmpty() const return length == 0;rn int Length() const return length;rn bool Find(int k, T& x) const;rn // return the k'th element of list in variable xrn int Search(const T& x) const; // return position of xrn LinearList& Delete(int k, T& x);rn // delete k'th element of list and return in xrn LinearList& Insert(int k, const T& x);rn // insert x just after k'th elementrn void Output(ostream& out) const;rn protected:rn int GetMaxSize() const return MaxSize;rn private:rn int length;rn int MaxSize;rn T *element; // dynamic arrayrn;rnrntemplaternLinearList::LinearList(int MaxListSize)rn// Create.rn MaxSize = MaxListSize;rn element = new T[MaxSize];rn length = 0;rnrnrntemplaternbool LinearList::Find(int k, T& x) constrn// Find k'th element.rn if (k < 1 || k > length) return false; // no k'thrn x = element[k - 1];rn return true;rnrnrntemplaternint LinearList::Search(const T& x) constrn// Locate x.rn for (int i = 0; i < length; i++)rn if (element[i] == x) return ++i;rn return 0;rnrnrntemplaternLinearList& LinearList::Delete(int k, T& x)rn// Delete k'th element.rn if (Find(k, x)) // move elements k+1, ..., downrn for (int i = k; i < length; i++)rn element[i-1] = element[i];rn length--;rn return *this;rn rn else throw OutOfBounds();rnrnrntemplaternLinearList& LinearList::Insert(int k, const T& x)rn// Insert x after k'th element.rn if (k < 0 || k > length) throw OutOfBounds();rn if (length == MaxSize) throw NoMem();rn // move one uprn for (int i = length-1; i >= k; i--)rn element[i+1] = element[i];rn element[k] = x;rn length++;rn return *this;rnrnrntemplaternvoid LinearList::Output(ostream& out) constrn// Put the list into the stream out.rn for (int i = 0; i < length; i++)rn out << element[i] << " ";rnrnrn// overload <rnostream& operator<<(ostream& out, const LinearList& x)rn x.Output(out); return out;rnrn#endifrnrn// file dstack.hrnrn#ifndef Stack_rn#define Stack_rnrn#include "llista.h"rn#include "xcept.h"rnusing namespace std;rnrnrntemplaternclass Stack : private LinearList rn// LIFO objectsrn public:rn Stack(int MaxStackSize = 10)rn : LinearList (MaxStackSize) rn bool IsEmpty() constrn return LinearList::IsEmpty();rn bool IsFull() constrn return (Length() == GetMaxSize());rn T Top() constrn if (IsEmpty()) throw OutOfBounds();rn T x; Find(Length(), x); return x;rn Stack& Add(const T& x)rn Insert(Length(), x); return *this;rn Stack& Delete(T& x)rn LinearList::Delete(Length(), x);rn return *this;rn;rnrn#endifrnrnrn在dev c++下编译,rn4 D:\guoxi\programs\c5\dstack.cpp In file included from dstack.cpp rn D:\guoxi\programs\c5\dstack.h In member function `bool Stack::IsFull() const': rn20 D:\guoxi\programs\c5\dstack.h there are no arguments to `Length' that depend on a template parameter, so a declaration of `Length' must be available rn20 D:\guoxi\programs\c5\dstack.h (if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated) rn20 D:\guoxi\programs\c5\dstack.h there are no arguments to `GetMaxSize' that depend on a template parameter, so a declaration of `GetMaxSize' must be available rn D:\guoxi\programs\c5\dstack.h In member function `T Stack::Top() const': rn23 D:\guoxi\programs\c5\dstack.h there are no arguments to `Length' that depend on a template parameter, so a declaration of `Length' must be availablern什么意思啊? 没有与模板参数有关的函数参数,所以函数应该说明。这个函数是基类中的函数啊。rn数据结构算法与应用的例程。 里面的俩个using namespace std; 是我加的。谢谢了。
typedef的问题
typedefrn下面是一个几乎所有人刚开始时都会答错的问题错误在于将typedef 当作宏扩展已rn知下面的typedefrntypedef char *cstring;rn在以下声明中cstr 的类型是什么rnextern const cstring cstr;rn第一个回答差不多都是rnconst char *cstrrn即指向const 字符的指针。但是这是不正确的const 。修饰cstr 的类型cstr 是一个指针,因此这个定义声明了cstr 是一个指向字符的const 指针rnchar *const cstr;rn我的回答也就是上文所说的错误的答案,但是我对正确的解释无法理解。请帮忙解答。谢谢。
?????????????typedef 的问题
我想把linux 下的c移到win下 rnrn在linux调试成功了rnrn在win下 出错 rnrnrn这是 一个头文件里面的 rn__extension__ typedef long long _off64_t; 我只知道 typedef long long _off64_t 是 定义一个64 位 的类型rn前面的 那个是什么我就不知道 了rn可以那么写吗??? rnrn头文件 用的是 linux 里面的rn下面是其他的错误rnrni:\平台移植\snaphu\snaphu-test\include\sys\_types.h(15) : error C2054: 在“__extension__”之后应输入“(”rni:\平台移植\snaphu\snaphu-test\include\sys\_types.h(15) : error C2085: “_off64_t” : 不在形参表中rni:\平台移植\snaphu\snaphu-test\include\sys\_types.h(18) : error C2085: “_ssize_t” : 不在形参表中rnc:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\stddef.h(74) : error C2082: 形参“errno”的重定义rnc:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\stddef.h(84) : error C2085: “intptr_t” : 不在形参表中rnc:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\stddef.h(93) : error C2085: “uintptr_t” : 不在形参表中rnc:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\stddef.h(102) : error C2085: “ptrdiff_t” : 不在形参表中rn
typedef问题
// rn// SortableStringArray.hrn// rnrntypedef int (__cdecl *GENERICCOMPAREFN)(const void * elem1, const void * elem2);rntypedef int (__cdecl *STRINGCOMPAREFN)(const CString * elem1, const CString * elem2);rnrnclass CSortableStringArray : public CStringArrayrnrnpublic:rn void Sort(STRINGCOMPAREFN pfnCompare = Compare);rnrnprotected:rn static int __cdecl Compare(const CString * pstr1, const CString * pstr2);rn;rnrn// rn// SortableStringArray.cpprn// rnrn#include "SortableStringArray.h"rnrnint CSortableStringArray::Compare(const CString * pstr1, const CString * pstr2)rnrn ASSERT(pstr1);rn ASSERT(pstr2);rn return pstr1->Compare(*pstr2);rnrnrnvoid CSortableStringArray::Sort(STRINGCOMPAREFN pfnCompare /*= CSortedStringArray::Compare */)rnrn CString * prgstr = GetData();rn qsort(prgstr,GetSize(),sizeof(CString),(GENERICCOMPAREFN)pfnCompare);rnrnrn[color=#FF0000]其中的 typedef int (__cdecl *GENERICCOMPAREFN)(const void * elem1, const void * elem2);rn typedef int (__cdecl *STRINGCOMPAREFN)(const CString * elem1, const CString * elem2);[/color]rn这两句什么意思?
类模板的问题!
templateclass abcdrn public:rn void gg(type,type);rn;rnrntemplatevoid gg(type a,type b)rn type c=a*b;rn cout< ho;rnrn ho.gg(1,2);rnreturn 1;rnrnrn编译的时候出错,提示说:rnrnha.obj : error LNK2001: unresolved external symbol "public: int __thiscall abcd::gg(int,int)" (?gg@?$abcd@H@@QAEHHH@Z)rnDebug/ha.exe : fatal error LNK1120: 1 unresolved externalsrnError executing link.exe.rnrn怎么回事?代码好象没错啊。
typedef 问题
typedef int (CVICALLBACK * PanelCallbackPtr)(int panel, int event, void *callbackData,rn int eventData1, int eventData2);rnrn什么意思?
typedef 的问题
typedef int (*PF) (const char *, const char *);rnrn  PF Register(PF pf);rn  Register() 的参数是一个 PF 类型的回调函数,返回某个函数的地址,其署名与先前注册的名字相同。做一次深呼吸。下面我展示一下如果不用 typedef,我们是如何实现这个声明的:rn  int (*Register (int (*pf)(const char *, const char *))) (const char *, const char *);rn这里是怎么展开成这样的 上面是不是漏了个括号????rnrn我的展开rn RF Register(PF pf);rn int (*Register)(RF pf,const char *) rn //PF pf为第一个参数 PF为Int 型 这里应该要强制转换吧,第二个参数为 const char *rnrnrn然后在展开 作为参数的 PF pfrn int (*Register)(const (char *)(int(*pf)(const char *,const char *)),const char *)rnrn有人指点下吗?rn
typedef 问题?
老师让我看代码,看到这个:rnrntypedef CTypedPtrList StringInfotList;rnrn< , >这是什么意思?我从没见到过啊.急!!
typedef的问题~
typedef int (*funcptr)();rnfuncptr table[10]; rn好象是funcptr 是函数指针?谁详细解释一下啊?
typedef问题!
这个程序:rntypedef char BookName[31];rntypedef char BookName[31]; //这两行什么意思?rnrnBookName bookOut[200]; //bookOut[200];什么形式?rnBookName borrower[200];rnBookName bookin; //bookin是什么形式哪?rnBookName name; //name是什么样的哪?rnrnif(bookOut[3]>bookin)rncout<