C++11 C++;变量未在作用域中声明

C++11 C++;变量未在作用域中声明,c++11,compiler-errors,C++11,Compiler Errors,在你将这个问题标记为重复问题之前,请注意,我确实看过这个帖子和网站上其他类似的帖子。我的问题不是理解变量作用域和/或预处理器如何工作。我已经记下来了 我无法在我的代码中找出这个讨厌的问题。我只是尝试用C++建模一个栈数据结构。编译器不断抱怨size和capacity未在此范围内声明(对于Stack.cpp)。有三个文件: Stack.h Stack.cpp Main.cpp 下面是头文件和.cpp文件的代码段: Stack.h #ifndef STACK_H_ #define STACK_H

在你将这个问题标记为重复问题之前,请注意,我确实看过这个帖子和网站上其他类似的帖子。我的问题不是理解变量作用域和/或预处理器如何工作。我已经记下来了

我无法在我的代码中找出这个讨厌的问题。我只是尝试用C++建模一个栈数据结构。编译器不断抱怨
size
capacity
未在此范围内声明(对于
Stack.cpp
)。有三个文件:

  • Stack.h
  • Stack.cpp
  • Main.cpp
下面是头文件和.cpp文件的代码段:

Stack.h

#ifndef STACK_H_
#define STACK_H_


#include <iostream>
#include <string>


using namespace std;


template<typename T> 
class Stack
{   
    public:
        Stack();
        void push(T item);
        T pop();

    private:
        int size;
        const int capacity = 10;
        T items[];  
};


#endif
#ifndef堆栈_
#定义堆栈_
#包括
#包括
使用名称空间std;
模板
类堆栈
{   
公众:
堆栈();
无效推送(T项);
T pop();
私人:
整数大小;
常数int容量=10;
T项目[];
};
#恩迪夫
Stack.cpp

#include "Stack.h"

using namespace std;

template<typename T>
Stack::Stack() : items[capacity]
{
    size = 0;
}


template<typename T>
void Stack::push(T item)
{
    if(size == capacity)
    {
        cout << "No more room left. Unable to add items.";
    }   
    else
    {
        items[size-1] = item;
        size++;
    }
}


template<typename T>
T Stack::pop()
{
    if(size == 0)
    {
        cout << "Stack is empty. There is nothing to remove.";
    }
    else
    {
        size--; 
    }
}
#包括“Stack.h”
使用名称空间std;
模板
堆栈::堆栈():项[容量]
{
尺寸=0;
}
模板
空堆栈::推送(T项)
{
如果(大小=容量)
{
库特
编译器还奇怪地抱怨“模板类堆栈”
在没有模板参数的情况下使用void Stack::push(T项)”

我认为编译器试图告诉您,在cpp文件中,您使用了
Stack
,您应该在其中使用
Stack
。例如,您的
push
声明应该如下所示

 template<typename T>
 void Stack<T>::push(T item)
 // ... etc ...
模板
空堆栈::推送(T项)
//……等等。。。

我检查了您的问题,修复了一些问题并进行了优化(只是一个建议),因此:

  • 第一个问题是,您模板化了整个类,而不仅仅是单独的函数,其他人以前回答过。因此,除了构造函数之外,您需要对每个函数使用模板列表
  • 第二个问题发生在您修复第一个问题时,编译器不会编译代码,因为您使用单独的文件来定义和实现(您不能用模板类来完成这一工作-至少不是以通常的方式)
  • 第三个也是最后一个问题出现在pop函数中,因为它是以这样的方式定义的,它必须返回一些东西,而在body中您不返回任何东西(我的更改只是临时修复)
  • 固定大小在推送中更改
  • 现在进行优化:

  • 使用命名空间std删除
  • 更改了用户可设置的容量(至少在堆栈实例化时)
  • 将项数组更改为std::unique_ptr智能指针,指向动态创建的数组
  • 将成员初始值设定项列表添加到构造函数
  • 代码(Stack.h):

    #ifndef堆栈_
    #定义堆栈_
    #包括
    #包含//以获得唯一的\u ptr
    //使用名称空间std;//最好不要使用名称空间,以避免将来出现问题
    模板
    类堆栈{
    公众:
    堆栈(const unsigned int&capacity=10);//通常使用用户可设置的容量定义堆栈
    无效推送(T项);
    T pop();
    私人:
    无符号整数大小;
    无符号整数容量;
    std::唯一的ptr项目;
    };
    //此类模板化函数(如下)不能在其他文件中实现
    模板
    堆栈::堆栈(常量无符号整数和容量):
    容量(容量),
    项目(新T【容量】,
    大小(0){}
    模板
    空堆栈::推送(T项){
    如果(大小=容量){
    std::cout将引导您向更好的方向发展。模板化的typename也是Stack。
    
    #ifndef STACK_H_
    #define STACK_H_
    
    #include <iostream>
    #include <memory> // for unique_ptr
    
    //using namespace std; // it is better to not use using namespace, for avoiding of problems in the future
    
    template<typename T>
    class Stack {
    public:
        Stack(const unsigned int& capacity = 10); // usually stacks are defined with user settable capacity
        void push(T item);
        T pop();
    private:
        unsigned int size;
        unsigned int capacity;
        std::unique_ptr<T[]> items;
    };
    
    // such templated functions (below) cannot be implemented in other file
    
    template<typename T>
    Stack<T>::Stack(const unsigned int& capacity) :
        capacity(capacity),
        items(new T[capacity]),
        size(0) {}
    
    template<typename T>
    void Stack<T>::push(T item) {
        if (size == capacity) {
            std::cout << "No more room left. Unable to add items." << std::endl;
        }
        else {
            items[size++] = item; // you should put item at index size, after that increase it by one
        }
    }
    
    template<typename T>
    T Stack<T>::pop() {
        // for such defined function you need to return something of type T
        if (size == 0) {
            std::cout << "Stack is empty. There is nothing to remove." << std::endl;
            return 0; // maybe return empty object?
        }
        else {
            return items[--size]; // probably return removed object
        }
    }
    
    #endif
    
    #include "Stack.h"
    
    int main() {
        Stack<int> stack;
        for (int e = 0; e < 11; e++) {
            stack.push(e);
        }
        for (int i = 0; i < 11; i++) {
            std::cout << stack.pop() << std::endl;
        }
        return 0;
    }