为什么我在尝试模板时遇到视觉工作室C++错误
Why I am getting visual studio C++ errors while trying Template
代码编译正确。然后当我尝试运行时出现这些构建错误
Error C2955 'Node': use of class template requires template argument list. Error C2514 'Node': class has no constructors
我对模板了解不多,所以无论我错在哪里,请纠正我。 所以下面的代码是我的头文件
#pragma once
#include<iostream>
using namespace std;
template<class T>
class Node
{
public:
Node *left, *right, *parent;
T key;
Node(){
left = right = parent = NULL;
}
};
template<class T>
class GenericBst {
Node<T>* root;
bool search(Node<T>* current, int value);//Finds a value is present
public:
GenericBst()
{
root = NULL;
}
void insertNode(int value);//Insertation of node
bool search(int value);
};
template<class T>
inline bool GenericBst<T>::search(Node<T>* current, int value)
{
if (current == NULL)
return false;
while (current != NULL)
{
if (current->key == value)
return true;
else
{
if (value < current->key)
current = current->left;
else
current = current->right;
}
}
}
template<class T>
inline void GenericBst<T>::insertNode(int value)
{
Node<T>*temp = new Node();
temp->key = value;
Node<T> *current = root, *parent = NULL;
while (current != NULL)
{
//Parent is assigned the current position and current goes to the child
parent = current;
if (value < current->key)
{
current = current->left;
}
else
{
current = current->right;
}
}
//Now we check if parent of current is NULL then currnt node is at root
if (parent == NULL)
{
root = temp;//temp was used to store the insertion value. Now root points to it.
}
else if (value < parent->key)
{
parent->left = temp;
}
else
{
parent->right = temp;
}
temp->parent = parent;
}
template<class T>
inline bool GenericBst<T>::search(int value)
{
return search(root,value);
}
主类有
#include<iostream>
using namespace std;
#include "GenericBst.h"
#include <iostream>
int main()
{
GenericBst<int> Obj;
//GenericBst<string> Obj;
std::cout << "Welcome to generic Worldn";
int i = 1;
int value;
while (i != 0)
{
cout << "nSelect:nt1. Insert a node.t2. Search a Nodent0. Quit" << endl;
switch (i)
{
case 0:
i = 0;
cout << "nttByen";
break;
case 1:
cout << "Enter value: ";
cin >> value;
Obj.insertNode(value);
break;
case 2:
cout << "Enter value: ";
cin >> value;
cout << Obj.search(value);
break;
}
}
}
应在模板类中显式指定模板参数。
Node<T>*temp = new Node();
必须是:
Node<T>*temp = new Node<T>();
相关文章:
- 编译包含字符串的代码时遇到问题
- 遇到新行时,有没有办法停止istream_iterator
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 视觉studo 2019中的漫画和静态/动态绑定
- 在 for 循环中查找问题时遇到困难
- 如何改变c++应用程序的视觉效果
- 创建结构的数组时遇到分段错误
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 视觉工作室项目.提取源文件夹名称
- 为什么我遇到分段错误?
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- 为什么我在尝试模板时遇到视觉工作室C++错误
- 如何从文本文件中读取数值,直到遇到字符类型?
- 从其他类访问类时遇到问题
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 视觉C++在解决循环依赖关系时遇到了设计问题
- 视觉我在C++中创建新对象时遇到麻烦
- 视觉我在C++的继承和多态性方面遇到了麻烦