【问题标题】:error C4430: missing type specifier - int assumed. Note: C++ does not support default-int towards my constructor [closed]错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持对我的构造函数的 default-int [关闭]
【发布时间】:2019-02-27 05:26:38
【问题描述】:

我现在一直在为模板和明显的类而苦苦挣扎。 它假定了我的构造函数的其他内容,并需要一个类型说明符。 这是目前我的程序中一大块问题的根源,如果这些信息有用,我正在使用 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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-03-28
      • 2022-12-04
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多