c++ - 错误 C4430 : missing type specifier - int assumed. 注意:C++ 不支持我的构造函数的默认整数

标签 c++ templates visual-c++ constructor

<分区>

我现在一直在努力使用模板和类。 它假定我的构造函数的其他内容并需要一个类型说明符。 这是目前我程序中一大块问题的根源,如果该信息有用,我正在使用 Visual Studio 2017。

这是保存编码的 .cpp 文件

#include "stacks.h"
#include "main.h"

template <class A_Type> Stack<A_Type>::stacks()
{
     top = -1;
};

template <class A_Type> A_Type Stack<A_Type>::push(A_Type x)
{
    //Checking if stack is full before pushing.
    if (top >= 8)
    {
        cout << "Stack Overflow \n";
    }
    //Stack is not full, then push int/float into stack.
    else
    {
        a[++top] = x;
        cout << "Element Inserted \n";
    }
}

template <class A_Type> A_Type Stack<A_Type>::pop()
{
    if (top < 0)
    {
        return 0;
    }
    else
    {
        A_Type d = a[top--];
        return d;
    }
}

template <class A_Type> A_Type Stack<A_Type>::peak()
{
    A_Type p = a[top];
    return p;
}

//wants to stay as template.
//Changed the A_Type behind Stack to an int. 
template <class A_Type> int Stack<A_Type>::count()
{
    int c = top + 1;
    return c;
}

template <class A_Type> A_Type Stack<A_Type>::IsEmpty()
{
    if (!count())
    {
        return true;
    }
    else
    {
        return false;
    }
}

template class Stack<int>;
template class Stack<float>;
template class Stack<void>;

这是类所在的 .h 文件。

#ifndef CODING03_STACKS_H
#define CODING03_STACKS_H

template <class A_Type> class Stack
{
private:
    int top; // count = top + 1

public:
    //Constructor
    stacks();

    A_Type a[9]; // Stack's max size.
    A_Type push(A_Type x);
    A_Type pop();
    A_Type peak();
    int count();
    A_Type IsEmpty();
};

#endif

我无法从类似的问题中找到答案。

最佳答案

您的构造函数称为 stacks而类(class)名为 Stack - 在 C++ 中,构造函数必须与类同名,即 Stack .

关于c++ - 错误 C4430 : missing type specifier - int assumed. 注意:C++ 不支持我的构造函数的默认整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54898568/

相关文章:

c++ - 使 QDialog 出现在不同的屏幕中

wpf - 可重复使用的多边形

html - Font Awesome 不适用于 HTML5 UP 模板

c++ - 指向模板类方法的指针的 Typedef

c++ - WIN32显示图像,为什么不显示?

C++ 静态变量和函数错误

c++ - 预处理之后和使用 CMake 编译之前的自定义构建步骤

c# - 托管代码 (C#) 与 Matlabs 和 C++ 的速度比较

visual-studio - 将stdout和stderr重定向到Microsoft Visual Studio的输出调试控制台

c++ - 加速字节模式扫描 C++