gpt4 book ai didi

c++ - 对象声明中的模板 "error: expected expression"

转载 作者:行者123 更新时间:2023-11-28 04:31:15 25 4
gpt4 key购买 nike

我在尝试声明使用模板类的新堆栈对象时收到错误代码。我似乎找不到与此相关的任何信息,而且我发现的大多数问题似乎都与创建函数有关。非常感谢任何有关如何解决此问题的帮助。

#include <stdio.h>
#include <iostream>
#include "stack4.h"

using namespace main_savitch_6B;
using namespace std;
int main(int argc, char **argv)
{
template <class Item> //error: expected expression
stack a;

a.push(4);



return 0;
}

我的头文件:

#ifndef MAIN_SAVITCH_STACK4_H
#define MAIN_SAVITCH_STACK4_H
#include <cstdlib> // Provides NULL and size_t
#include "node2.h" // Node template class from Figure 6.5 on page 308

namespace main_savitch_6B //7B
{
template <class Item>
class stack
{
public:
// TYPEDEFS
typedef std::size_t size_type;
typedef Item value_type;
// CONSTRUCTORS and DESTRUCTOR
stack( ) { top_ptr = NULL; }
stack(const stack& source);
~stack( ) { list_clear(top_ptr); }
// MODIFICATION MEMBER FUNCTIONS
void push(const Item& entry);
void pop( );
void operator =(const stack& source);
Item& top( );
void swap(stack& y);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const
{ return main_savitch_6B::list_length(top_ptr); }
bool empty( ) const { return (top_ptr == NULL); }
const Item& top( ) const;

private:
main_savitch_6B::node<Item> *top_ptr; // Points to top of stack
};
}

#include "stack4.template" // Include the implementation
#endif

最佳答案

stack<int> a;

如果你想定义一个模板类的实例

关于c++ - 对象声明中的模板 "error: expected expression",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52827274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com