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