|
蓝森林 http://www.lslnet.com 2006年6月6日 10:18
C++程序问题求助?
本人写了个单向链表的类,但是在LINUX下GCC编译报错误
代码如下
ChainNode.h
[code]
#include <iostream>;
using namespace std;
template <typename T>;
class ChainNode{
friend Chain<T>;;
private:
T *data;
ChainNode *link;
};
template <typename T>;
class Chain{
public:
Chain(){ first=0;}
~Chain();
bool IsEmpty(){ return first == 0; }
int Length()const;
bool Find(int n,T &elem);
int Search(const T &elem);
Chain<T>;& Insert(int n,const T &elem);
Chain<T>;& Delete(int n,T &elem);
void OutPut(ostream&) const
ostream& operator<<(ostream &out,const Chain<T>; &elem);
private:
ChainNode<T>; *first;
};
[/code]
ChainNode.C
[code]
#include "ChainNode.h"
using namespace std;
template <typename T>;
ostream& Chain<T>;::operator<<(ostream &out,const Chain<T>; &elem)
{
elem.Output(out);
return out;
}
int main(int argc,char** argv)
{
return 0;
}
template <typename T>;
Chain<T>;::~Chain()
{
ChainNode<T>; *next;
while(first){
next = first->;link;
delete first;
first = next;
}
}
template <typename T>;
int Chain<T>;::Length()const
{
int Len = 0;
ChainNode<T>; *Current=first;
while(first){
Len++;
Current = Current->;link;
}
return Len;
}
template <typename T>;
bool Chain<T>;::Find(int n,T &elem)
{
if ( n < 1) return false;
ChainNode<T>; *Current = first;
int Index = 1;
while(Index < n && Current ){
Current = Current->;link;
Index++;
}
if ( Current ){
elem = Current->;data;
return true;
}
return false;
}
template <typename T>;
int Chain<T>;::Search(const T &elem)
{
int Index = 1;
ChainNode<T>; *Current = first;
while(Current && Current->;data!=elem){
Current = Current->;link;
Index++;
}
if ( Current ) return Index;
return 0;
}
template <typename T>;
void Chain<T>;::OutPut(ostream &out)const
{
ChainNode<T>; *Current = first;
while(Current){
Current = Current->;link;
out << Current->;data << " ";
}
}
template <typename T>;
Chain<T>;& Chain<T>;::Delete(int n,T &elem)
{
if ( n < 1 && !first )
exit(-1);
ChainNode<T>; *p = first;
if ( n==1 )
first = first->;link;
else{
ChainNode<T>; *q = first;
for(int index = 1;index < n-1 && q ; index++)
q = q->;link;
if ( !q || !q->;link )
exit(-1);
p = q->;link;
q->;link = p->;link;
}
elem = p->;data;
delete p;
return *this;
}
template <typename T>;
Chain<T>;& Chain<T>;::Insert(int n,const T &elem)
{
if ( n < 1 && !first )
exit(-1);
ChainNode<T>; *p = first;
ChainNode<T>; *NewNode = new ChainNode<T>;;
NewNode->;data = elem;
if ( n==1 ){
NewNode->;link = p;
first = NewNode;
}
else{
ChainNode<T>; *q = first;
for(int index = 1; index < n-1 && q; index++)
q=q->;link;
if ( !q || !q->;link )
exit(-1);
p = q->;link;
q->;link = NewNode;
NewNode->;link = p;
}
return *this;
}
[/code]
编译错误为:
[code]
ChainNode.h:7: ISO C++ forbids declaration of `Chain' with no type
ChainNode.h:7: template-id `Chain<T>;' used as a declarator
ChainNode.h:7: `Chain' is neither function nor member function; cannot be
declared friend
ChainNode.h:25: parse error before `&' token
ChainNode.C:8: `std::basic_ostream<char, std::char_traits<char>; >;&
Chain<T>;::operator<<(std::basic_ostream<char, std::char_traits<char>; >;&,
const Chain<T>;&)' must take exactly one argument
ChainNode.C:8: no `std::basic_ostream<char, std::char_traits<char>; >;&
Chain<T>;::operator<<(std::basic_ostream<char, std::char_traits<char>; >;&,
const Chain<T>;&)' member function declared in class `Chain<T>;'
ChainNode.C:8: template definition of non-template `std::basic_ostream<char,
std::char_traits<char>; >;& Chain<T>;::operator<<(std::basic_ostream<char,
std::char_traits<char>; >;&, const Chain<T>;&)'
[/code]
谢谢指点!! |
C++程序问题求助?
.h 与.c写在一起是是 |
C++程序问题求助?
早就尝试过了,不行!
请各位看看,指点迷津:) |
C++程序问题求助?
你可以按你的编译错误信息来更改错误的.
看不懂吗? |
C++程序问题求助?
[code]#include <iostream>;
using namespace std;
template <typename T>;
class Chain;
template <typename T>;
class ChainNode{
friend class Chain<T>;;
private:
T *data;
ChainNode *link;
};
template <typename T>;
class Chain{
public:
Chain(){ first=0;}
~Chain();
bool IsEmpty(){ return first == 0; }
int Length()const;
bool Find(int n,T &elem);
int Search(const T &elem);
Chain<T>;& Insert(int n,const T &elem);
Chain<T>;& Delete(int n,T &elem);
void OutPut(ostream&) const ;
friend ostream& operator<<(ostream &out,const Chain<T>; &elem);
private:
ChainNode<T>; *first;
};
[/code]
[code]#include "ChainNode.h"
using namespace std;
template <typename T>;
ostream& operator<<(ostream &out,const Chain<T>; &elem)
{
elem.Output(out);
return out;
}
int main(int argc,char** argv)
{
return 0;
}
template <typename T>;
Chain<T>;::~Chain()
{
ChainNode<T>; *next;
while(first){
next = first->;link;
delete first;
first = next;
}
}
template <typename T>;
int Chain<T>;::Length()const
{
int Len = 0;
ChainNode<T>; *Current=first;
while(first){
Len++;
Current = Current->;link;
}
return Len;
}
template <typename T>;
bool Chain<T>;::Find(int n,T &elem)
{
if ( n < 1) return false;
ChainNode<T>; *Current = first;
int Index = 1;
while(Index < n && Current ){
Current = Current->;link;
Index++;
}
if ( Current ){
elem = Current->;data;
return true;
}
return false;
}
template <typename T>;
int Chain<T>;::Search(const T &elem)
{
int Index = 1;
ChainNode<T>; *Current = first;
while(Current && Current->;data!=elem){
Current = Current->;link;
Index++;
}
if ( Current ) return Index;
return 0;
}
template <typename T>;
void Chain<T>;::OutPut(ostream &out)const
{
ChainNode<T>; *Current = first;
while(Current){
Current = Current->;link;
out << Current->;data << " ";
}
}
template <typename T>;
Chain<T>;& Chain<T>;::Delete(int n,T &elem)
{
if ( n < 1 && !first )
exit(-1);
ChainNode<T>; *p = first;
if ( n==1 )
first = first->;link;
else{
ChainNode<T>; *q = first;
for(int index = 1;index < n-1 && q ; index++)
q = q->;link;
if ( !q || !q->;link )
exit(-1);
p = q->;link;
q->;link = p->;link;
}
elem = p->;data;
delete p;
return *this;
}
template <typename T>;
Chain<T>;& Chain<T>;::Insert(int n,const T &elem)
{
if ( n < 1 && !first )
exit(-1);
ChainNode<T>; *p = first;
ChainNode<T>; *NewNode = new ChainNode<T>;;
NewNode->;data = elem;
if ( n==1 ){
NewNode->;link = p;
first = NewNode;
}
else{
ChainNode<T>; *q = first;
for(int index = 1; index < n-1 && q; index++)
q=q->;link;
if ( !q || !q->;link )
exit(-1);
p = q->;link;
q->;link = NewNode;
NewNode->;link = p;
}
return *this;
} [/code]
有两个警告还没看.......
要上课了...:) |
C++程序问题求助?
为什么然而dev-cpp4.9.8下面编译还有warning?
18 D:\NOTE\DEV\ABP\ChainNode.h
[Warning] friend declaration `std::ostream&
18 D:\NOTE\DEV\ABP\ChainNode.h
[Warning] (if this is not what you intended,
我觉得friend这个函数没什么问题吧? |
C++程序问题求助?
把friend ostream& operator<<(ostream &out,const Chain<T>; &elem)
改成:
friend ostream& operator<< <>;(ostream &out,const Chain<T>; &elem)
因为我们声明的是一个模板. |
C++程序问题求助?
为什么不把两个类的代码封装在一起?使用friend关键子有悖C++的设计思想。只有在不得已的情况下才使用这个特性。 |
C++程序问题求助?
| |
|