首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对`Stack<char>::Stack()的未定义引用

对`Stack<char>::Stack()的未定义引用
EN

Stack Overflow用户
提问于 2016-10-18 14:49:54
回答 1查看 1.8K关注 0票数 2

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

误差信息

代码语言:javascript
运行
复制
obj\Debug\main.o||In function `main':|
E:\project\test\main.cpp|15|undefined reference to `Stack<char>::Stack()'|
E:\project\test\main.cpp|23|undefined reference to `Stack<char>::shiftL()'|
E:\project\test\main.cpp|28|undefined reference to `Stack<char>::shiftR()'|
E:\project\test\main.cpp|33|undefined reference to `Stack<char>::Del()'|
E:\project\test\main.cpp|36|undefined reference to `Stack<char>::push(char)'|
||error: ld returned 1 exit status|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Stack.h

代码语言:javascript
运行
复制
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED

template <class Type>
struct List
{
    Type content;
    List<Type> *head;
    List<Type> *end;
};

template <class Type>
class Stack
{
public:
    Stack();
    ~Stack(){};
    void push(Type x);
    void Del();
    void shiftL();
    void shiftR();
    List<Type> *Front(){ return front; }
    List<Type> *Back(){ return back; }
private:
    List<Type> *front;
    List<Type> *back;
    List<Type> *operate;
    List<Type> F;
    List<Type> B;
    int pos;
};
#endif // STACK_H_INCLUDED

Stack.cpp

代码语言:javascript
运行
复制
#include"Stack.h"
#include<stdio.h>
template<class Type>
Stack<Type>::Stack()
{
    F.head = B.end = NULL;
    front = &F;
    back = &B;
    front->end = &B;
    back->head = &F;
    operate = back;
    pos = 0;
}
template<class Type>
void Stack<Type>::Del()
{
    if (pos>0)
    {
        List<Type>* temp;
        temp = operate;
        operate = operate->end;
        operate->head = temp->head;
        temp->head->end = operate;
        pos--;
    }
}
template<class Type>
void Stack<Type>::shiftL()
{
    if (pos>0)
    {
        operate = operate->end;
        pos--;
    }

}
template<class Type>
void Stack<Type>::shiftR()
{
    if (operate->head != front)
    {
        operate = operate->head;
        pos++;
    }
}
template<class Type>
void Stack<Type>::push(Type x)
{

    List<Type>* temp;
    temp = new List<Type>;
    temp->content = x;
    temp->end = operate;
    temp->head = operate->head;
    operate->head->end = temp;
    operate->head = temp;
    operate = operate->head;
    pos++;
}

mian.cpp

代码语言:javascript
运行
复制
#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"中添加main.cpp(这是非法的,对吗?),编译器不会显示任何错误或警告。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-18 14:53:34

由于Stack是一个模板,编译器无法在链接时生成它,它需要知道编译时将用哪个模板参数实例化。

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

代码语言:javascript
运行
复制
#include "Stack.cpp"

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

代码语言:javascript
运行
复制
template class Stack<char>;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40111459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档