
为什么我在包含Main函数的Cpp文件定义变量没有错,而在另一个头文件声明却报错呢?
#ifndefLINKSTACK_H#defineLINKSTACK_Htemplate<classObject>classLinkStack{private:struc...
#ifndef LINKSTACK_H
#define LINKSTACK_H
template<class Object>
class LinkStack
{
private:
struct Node
{
Object data;
Node *prev;
Node *next;
Node( Object d = Object(), Node *p = NULL, Node *n = NULL )
: data( d ), prev( p ), next( n ){}
};
public:
class iterator
{
private:
Node *current;
Object& retrieve() const
{ return current->data; }
iterator( Node* p ): current( p ) {}
public:
iterator(): current( NULL ) {}
Object& operator* ()
{ return retrieve(); }
iterator& operator++ ()
{
current = current->next;
return *this;
}
iterator operator++ ( int )
{
iterator old = *this;
++( *this );
return old;
}
bool operator= ( iterator rhs )
{ return rhs.current == current; }
bool operator!= ( iterator rhs )
{ return !( *this == rhs ); }
friend class LinkStack<Object>;
};
LinkStack()
{
theSize = 0;
head = new Node( Object(), NULL, NULL );
tail = new Node( Object(), NULL, NULL );
head->next = tail;
tail->prev = head;
}
Object shift()
{ return erase( head->next ); }
Object pop()
{ return erase( tail->prev ); }
void push( Object d )
{ insert( tail, d ); }
void unshift( Object d )
{ insert( head->next, data ); }
iterator begin()
{ return head->next; }
iterator end()
{ return tail->prev; }
int size()
{ return theSize; }
bool empty()
{ return size() == 0; }
void clear()
{
while( !empty() )
erase( tail->prev );
}
private:
Object erase( Node *p )
{
theSize--;
Object d = p->data;
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
return d;
}
void insert( Node *p, Object d )
{
theSize++;
p->prev = p->prev->next = new Node( d, p->prev, p );
}
int theSize;
Node *head, *tail;
};
//***********StdFunLib********************
#ifndef STDFUNLIB_H
#define STDFUNLIB_H
#include"LinkStack.h"
LinkStack<char*> stdFunLib;
stdFunLib.push( "sin" );
stdFunLib.push( "cos" );
int a = 5;
#endif
#endif
//*********Main.cpp**********
#include<iostream>
#include"LinkStack.h"
#include"StdFunLib.h"
using namespace std;
int main()
{
cout<<stdFunLib.shift()<<endl;
cout<<*stdFunLib.end()<<endl;
cout<<a<<endl;
getchar();
getchar();
return 0;
}
错误消息如下:
d:\workspace\gui\gui\stdfunlib.h(6): error C2143: 语法错误 : 缺少“;”(在“.”的前面)
d:\workspace\gui\gui\stdfunlib.h(6): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
d:\workspace\gui\gui\stdfunlib.h(6): error C2371: “stdFunLib”: 重定义;不同的基类型
d:\workspace\gui\gui\stdfunlib.h(5) : 参见“stdFunLib”的声明
d:\workspace\gui\gui\stdfunlib.h(7): error C2143: 语法错误 : 缺少“;”(在“.”的前面)
d:\workspace\gui\gui\stdfunlib.h(7): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
d:\workspace\gui\gui\stdfunlib.h(7): error C2371: “stdFunLib”: 重定义;不同的基类型
d:\workspace\gui\gui\stdfunlib.h(5) : 参见“stdFunLib”的声明
求教,拜托了!!!!!
我在Main函数里面定义LinkStack<char*> stdFunLib;
stdFunLib.push( "sin" );
stdFunLib.push( "cos" );
不会报错,输出:
sin
cos
各位大侠们,不要被那么长的LinkStack吓到了,那可也跳过,我可以保证这个类里面没有任何问题。我只是奇怪,为什么声明的地方不同,结果竟会有如此的不同。还有为什么无论在头文件还是在Main函数声明int型变量,都不会报错,而声明LinkStack却报错。求教啊!!!!!!!!! 展开
#define LINKSTACK_H
template<class Object>
class LinkStack
{
private:
struct Node
{
Object data;
Node *prev;
Node *next;
Node( Object d = Object(), Node *p = NULL, Node *n = NULL )
: data( d ), prev( p ), next( n ){}
};
public:
class iterator
{
private:
Node *current;
Object& retrieve() const
{ return current->data; }
iterator( Node* p ): current( p ) {}
public:
iterator(): current( NULL ) {}
Object& operator* ()
{ return retrieve(); }
iterator& operator++ ()
{
current = current->next;
return *this;
}
iterator operator++ ( int )
{
iterator old = *this;
++( *this );
return old;
}
bool operator= ( iterator rhs )
{ return rhs.current == current; }
bool operator!= ( iterator rhs )
{ return !( *this == rhs ); }
friend class LinkStack<Object>;
};
LinkStack()
{
theSize = 0;
head = new Node( Object(), NULL, NULL );
tail = new Node( Object(), NULL, NULL );
head->next = tail;
tail->prev = head;
}
Object shift()
{ return erase( head->next ); }
Object pop()
{ return erase( tail->prev ); }
void push( Object d )
{ insert( tail, d ); }
void unshift( Object d )
{ insert( head->next, data ); }
iterator begin()
{ return head->next; }
iterator end()
{ return tail->prev; }
int size()
{ return theSize; }
bool empty()
{ return size() == 0; }
void clear()
{
while( !empty() )
erase( tail->prev );
}
private:
Object erase( Node *p )
{
theSize--;
Object d = p->data;
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
return d;
}
void insert( Node *p, Object d )
{
theSize++;
p->prev = p->prev->next = new Node( d, p->prev, p );
}
int theSize;
Node *head, *tail;
};
//***********StdFunLib********************
#ifndef STDFUNLIB_H
#define STDFUNLIB_H
#include"LinkStack.h"
LinkStack<char*> stdFunLib;
stdFunLib.push( "sin" );
stdFunLib.push( "cos" );
int a = 5;
#endif
#endif
//*********Main.cpp**********
#include<iostream>
#include"LinkStack.h"
#include"StdFunLib.h"
using namespace std;
int main()
{
cout<<stdFunLib.shift()<<endl;
cout<<*stdFunLib.end()<<endl;
cout<<a<<endl;
getchar();
getchar();
return 0;
}
错误消息如下:
d:\workspace\gui\gui\stdfunlib.h(6): error C2143: 语法错误 : 缺少“;”(在“.”的前面)
d:\workspace\gui\gui\stdfunlib.h(6): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
d:\workspace\gui\gui\stdfunlib.h(6): error C2371: “stdFunLib”: 重定义;不同的基类型
d:\workspace\gui\gui\stdfunlib.h(5) : 参见“stdFunLib”的声明
d:\workspace\gui\gui\stdfunlib.h(7): error C2143: 语法错误 : 缺少“;”(在“.”的前面)
d:\workspace\gui\gui\stdfunlib.h(7): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
d:\workspace\gui\gui\stdfunlib.h(7): error C2371: “stdFunLib”: 重定义;不同的基类型
d:\workspace\gui\gui\stdfunlib.h(5) : 参见“stdFunLib”的声明
求教,拜托了!!!!!
我在Main函数里面定义LinkStack<char*> stdFunLib;
stdFunLib.push( "sin" );
stdFunLib.push( "cos" );
不会报错,输出:
sin
cos
各位大侠们,不要被那么长的LinkStack吓到了,那可也跳过,我可以保证这个类里面没有任何问题。我只是奇怪,为什么声明的地方不同,结果竟会有如此的不同。还有为什么无论在头文件还是在Main函数声明int型变量,都不会报错,而声明LinkStack却报错。求教啊!!!!!!!!! 展开
展开全部
问题有2个
1。你的那个头文件防止重复包含的
#ifndef LINKSTACK_H#define LINKSTACK_H
#endif
main里面包含了一次该头文件了,相当于定义了LINKSTACK_H,出错的头文件再次包含该头文件判断LINKSTACK_H是否定义,由于它已经定义,所以就跳过了头文件的内容,造成后面使用该类型认为没定义
2。函数调用应该放在函数里面,不要那样调用
1。你的那个头文件防止重复包含的
#ifndef LINKSTACK_H#define LINKSTACK_H
#endif
main里面包含了一次该头文件了,相当于定义了LINKSTACK_H,出错的头文件再次包含该头文件判断LINKSTACK_H是否定义,由于它已经定义,所以就跳过了头文件的内容,造成后面使用该类型认为没定义
2。函数调用应该放在函数里面,不要那样调用
更多追问追答
追问
可是这正是#ifndef、#endif的功能啊,要不然定义了一个类,然后只能导入到一个文件内。我觉得它可以避免Main重复函数导入LinkStack类,但StdFunLib头文件是可以导入LinkStack类的,所以不是#ifndef的问题。大师,你告诉怎么解决这个问题吧,这样我也好实践检验一下,谢谢了。
追答
那为什么没有考虑一下先后顺序呢:)
LinkStack stdFunLib;
stdFunLib.push( "sin" );/////这种函数调用语句不能放在这种地方,得通过函数调用执行,之前说过了
stdFunLib.push( "cos" );
int a = 5;
main.cpp中
#include"LinkStack.h"//////把这行删了就行了,因为"StdFunLib.h"里面已经包含LinkStack.h了
#include"StdFunLib.h"//
“我在Main函数里面定义LinkStack stdFunLib;
stdFunLib.push( "sin" );
stdFunLib.push( "cos" );
不会报错"
---当然不会报错,两个问题都规避了,1,不用头文件,不再外面调用函数
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询