Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 未定义对“堆栈”的引用<;char>;::堆栈()';_C++ - Fatal编程技术网

C++ 未定义对“堆栈”的引用<;char>;::堆栈()';

C++ 未定义对“堆栈”的引用<;char>;::堆栈()';,c++,C++,使用mingw32-g++在代码块中编译pj时遇到了一个问题 错误msgs 函数“main”中的obj\Debug\main.o || E:\project\test\main.cpp | 15 |对“Stack::Stack()”的未定义引用| E:\project\test\main.cpp | 23 |对“Stack::shiftL()的未定义引用”| E:\project\test\main.cpp | 28 |对“Stack::shiftR()的未定义引用”| E:\project\t

使用mingw32-g++在代码块中编译pj时遇到了一个问题

错误msgs 函数“main”中的obj\Debug\main.o || E:\project\test\main.cpp | 15 |对“Stack::Stack()”的未定义引用| E:\project\test\main.cpp | 23 |对“Stack::shiftL()的未定义引用”| E:\project\test\main.cpp | 28 |对“Stack::shiftR()的未定义引用”| E:\project\test\main.cpp | 33 |对“Stack::Del()的未定义引用”| E:\project\test\main.cpp | 36 |对`堆栈::推送(字符)'的未定义引用| ||错误:ld返回了1个退出状态| ||==生成失败:6个错误,0个警告(0分钟,0秒))===| Stack.h
#如果包含NDEF堆栈#
#定义包含的堆栈
模板
结构列表
{
类型内容;
名单*标题;
列表*结束;
};
模板
类堆栈
{
公众:
堆栈();
~Stack(){};
空心推力(x型);
void Del();
void-shiftL();
void shiftR();
List*Front(){return Front;}
List*Back(){return Back;}
私人:
列表*正面;
列表*返回;
列表*操作;
清单F;
名单B;
int pos;
};
#endif//包括堆栈
Stack.cpp
#包括“Stack.h”
#包括
模板
Stack::Stack()
{
F.head=B.end=NULL;
前=&F;
后退=&B;
前端->结束=&B;
背面->头部=&F;
操作=返回;
pos=0;
}
模板
void Stack::Del()
{
如果(位置>0)
{
列表*临时;
温度=运行温度;
操作=操作->结束;
操作->压头=温度->压头;
温度->头部->末端=操作;
pos--;
}
}
模板
void Stack::shiftL()
{
如果(位置>0)
{
操作=操作->结束;
pos--;
}
}
模板
void Stack::shiftR()
{
如果(操作->头部!=前部)
{
操作=操作->头部;
pos++;
}
}
模板
空堆栈::推送(类型x)
{
列表*临时;
临时=新列表;
温度->内容=x;
温度->结束=运行;
温度->压头=操作->压头;
操作->头部->末端=温度;
操作->压头=温度;
操作=操作->头部;
pos++;
}

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

int main()
{
    char c;
    int k;
    cin >> k;
    getchar();
    for (int j = 0; j < k; j++)
    {
        Stack<char> password;
        while (1)
        {
            c = getchar();
            if (c == '\n')
                break;
            if (c == '<')
            {
                password.shiftL();
                continue;
            }
            if (c == '>')
            {
                password.shiftR();
                continue;
            }
            if (c == '-')
            {
                password.Del();
                continue;
            }
            password.push(c);
        }

            List<char>* i;
            for (i = password.Back()->head; i != password.Front(); i = i->head)
                printf("%c", i->content);
            printf("\n");
    }
    return 0;
}
#包括
#包括
#包括“Stack.h”
使用名称空间std;
int main()
{
字符c;
int k;
cin>>k;
getchar();
对于(int j=0;jhead;i!=password.Front();i=i->head)
printf(“%c”,i->content);
printf(“\n”);
}
返回0;
}

但是 如果我在main.cpp中添加
#include“Stack.cpp”
(这是非法的,对吗?),编译器不会显示任何错误或警告。

你能告诉我为什么吗?非常感谢!:)

给定
堆栈
是编译器在链接时无法生成的模板,它需要知道编译时将使用哪些模板参数进行实例化

你有几种方法可以做到这一点。第一种方法是将函数定义移动到头文件中(或者在头文件中等效地包含合法的cpp文件)。 在堆栈的末尾。h:

#include "Stack.cpp"
另一种方法是执行显式模板实例化,告诉编译器所有可能在
stack.cpp
中使用的模板参数,然后允许编译器在编译时生成这些参数并正常链接。在堆栈.cpp的末尾:

template class Stack<char>;
模板类栈;

我找到了另一种方法。只需将
Stack.cpp
更改为
Stack.hpp
,并在
Stack.h
中添加
#包括“Stack.hpp”
,编译器就会预编译它。无论如何,谢谢你!:)
#include<stdio.h>
#include<iostream>
#include"Stack.h"
using namespace std;

int main()
{
    char c;
    int k;
    cin >> k;
    getchar();
    for (int j = 0; j < k; j++)
    {
        Stack<char> password;
        while (1)
        {
            c = getchar();
            if (c == '\n')
                break;
            if (c == '<')
            {
                password.shiftL();
                continue;
            }
            if (c == '>')
            {
                password.shiftR();
                continue;
            }
            if (c == '-')
            {
                password.Del();
                continue;
            }
            password.push(c);
        }

            List<char>* i;
            for (i = password.Back()->head; i != password.Front(); i = i->head)
                printf("%c", i->content);
            printf("\n");
    }
    return 0;
}
#include "Stack.cpp"
template class Stack<char>;