求助:C++在头文件中用枚举变量作为类中函数返回值一直有错??为什么??!

Cozeh 2019-09-07 08:42:36
数据结构与算法的作业,快把自己逼疯了,网上有效信息好少(或者是我自己不会筛选??)。
一直这样报我看不懂的错,叹息.....

第一次求助广大网友,求教



感觉出问题的原因应该是:在头文件里定义了枚举变量Error_code。
但我也试了在头文件里声明static,在.cpp文件里定义;试了在头文件里定义,在.cpp文件里用extern声明,都不行。
甚至我还试了将枚举变量移到了类的内部,都不行。
awsl,数据结构杀我

代码如下:

这个是关于栈的头文件:

// stack.h
// stack头文件

#ifndef stack_h
#define stack_h
#include<iostream>
using namespace std;

enum Error_code {underflow,success,overflow};

template <typename Stack_entry>
class Stack
{
public:
Stack(int len) : count(0), size(len), entry(new Stack_entry[size]) {}
~ Stack() { delete [] entry; }
bool empty() const;
Error_code pop();
Error_code top( Stack_entry &item ) const;
Error_code push(const Stack_entry &item);
private:
int count;
int size;
Stack_entry * entry;
};


#endif


这个是栈的,cpp文件:

// stack.cpp
// stack的具体实现文件

#include<iostream>
#include "stack.h"
using namespace std;

template <class Stack_entry>
bool Stack<Stack_entry> :: empty() const
/*Pre: None.
Post:If the stack is empty, true is returned. Otherwisw false is returned.*/
{
return count == 0 ? true : false;
}

template <class Stack_entry>
Error_code Stack<Stack_entry> :: pop()
/*Pre: None.
Post:If the stack is not empty, the top of the stack is removed.
If the stack is empty, an Error_code of underflow is returned.*/
{
Error_code outcome = success;
if (count == 0)
outcome = underflow;
else
cout << entry[count--];
return outcome;
}

template <class Stack_entry>
Error_code Stack<Stack_entry> :: push(const Stack_entry &item)
/*Pre: None.
Post:If the stack is not full, item is added to the top of the stack..
If the stack is full, an Error_code of overflow is returned.*/
{
Error_code outcome = success;
if (count > size)
outcome = overflow;
else
entry[count++] = item;
return outcome;
}

template <class Stack_entry>
Error_code Stack<Stack_entry> :: top(Stack_entry &item) const
/*Pre: None.
Post:If the stack is not empty, the top of the stack is returned in item.
If the stack is empty, an Error_code of underflow is returned.*/
{

Error_code outcome = success;
if(count == 0)
outcome = underflow;
else
item = empty[count-1];
return outcome;
}


然后捏,这个是主程序:

/* 程序目的:
用模板形式将动态数组存储的栈实现
在主函数中将栈实例化
完成指定输入输出测试程序
*/

#include<iostream>
#include"stack.h"
using namespace std;

int main()
{
Stack<char> data(3);
data.push('a');
data.push('b');
data.push('c');
data.pop();
data.pop();
data.pop();

return 0;

}


问题到底在哪里?
...全文
300 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
XM3990205HZ2552 2019-09-08
  • 打赏
  • 举报
回复
把实现写在.h里面
uouo88 2019-09-08
  • 打赏
  • 举报
回复
Templates实现通常被置于头文件内,因为编译器在编译期间就需要对模版类实例化(C++大部分编译器如此),而实例化的过程中,编译器需要知道它长什么样子。具体请参考下面文章:
https://blog.csdn.net/caoshangpa/article/details/53389234
Italink 2019-09-07
  • 打赏
  • 举报
回复
模板类需要将声明和定义都放在同一(头)文件中,如果编译器支持局部编译模式 ,可以使用export关键字

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧