c++ - 定义一个只有标题的类 - 找不到不存在的 cpp

标签 c++ templates header

我的目标是通过我的链表创建我自己的堆栈,它可以作为整数、字符串等的模板。下面是我的两个重要文件。

Main.cpp

#include <iostream>
#include <string>
#include "Stack.h"

using namespace std;

int main()
{
    Stack<int>* myStack = new Stack<int>();
    myStack->add(5);

    system("pause");
    return 0;
}

Stack.h

#pragma once

#include <iostream>
#include <string>
#include "LinkedList.h"

using namespace std;

template <class T>
class Stack 
{
public:
    Stack()
    {}
    ~Stack()
    {}

    void add(const T& val);

    ....

private:
    LinkedList<T> myStack;
};

template<class T>
inline void Stack<T>::add(const T& val)
{
    myStack.newHead(val);
}

....

我最初将 Stack 的实现放入它自己的 .cpp 中,但我遇到了问题,建议我定义与 .h 内联的成员函数。奇怪的是,每当我运行 Main.cpp 时,我都会得到:

"error C1083: Cannot open source file: 'Stack.cpp': No such file or directory."

这是我第一次尝试在没有 .cpp 的情况下创建类 - 我忽略了什么?

最佳答案

从项目中删除“Stack.cpp”。它在解决方案资源管理器中。

关于c++ - 定义一个只有标题的类 - 找不到不存在的 cpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16876465/

相关文章:

c++ - RenderWindow.display() 导致 sfml 崩溃

c++ - 将 lambda 作为参数注入(inject)到模板方法中

c++ - 返回一个打开的套接字连接/保留一个打开的套接字连接 C++

c++ - 扩展 Eigen Ref 类

c++ - 错误 C2893 : Failed to specialize function template VC++ 2012

css - style.css 在 wordpress 中不起作用

html - 调整窗口大小时标题图像停止重复

css - CSS 页眉周围的空格

c++ - `const` 到非 `const` 或非 `const` 到 `const`

c++ - 结合 OpenMP、Intel MKL 和 MSVC 编译器时出现巨大内存泄漏