标识符“stack”(我的类)未定义,即使标头已正确包含

问题描述 投票:0回答:2

在我的源文件中,当我定义对象时,Visual Studios 说标识符“stack”未定义。我很确定我已经正确分隔了标题,但我不知道为什么会收到此错误。另请注意,当我将所有内容放在一个源文件中并编译它时,它会立即退出,没有任何原因。感谢您提前的帮助。

这是源文件

// pa3.cpp : Defines the entry point for the console application.
//

#include "stack.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
//#include <ctype.h>

using namespace std;

int main()
{
    //int count;
    stack s; //assign s object to stack

    string input;
    cout << "Please enter the name of the input file: \n";
    //cin >> input;

    getline(cin, input);
    ifstream file(input);
    string readline;

    //ifstream file(input);

    while (getline(file, readline)) //take first line of file and assign to readline
    {
        s.push(readline); //send it off to push

        s.retrieveUPPER();
        //file.close();
        system("pause");
    }



    return 0;
}

这是stack.h文件

//#pragma once
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

//#include <ctype.h>

using namespace std;

class stack
{
public:
    int count;
    void push(string);
    //void pop();
    void check(string);
    void retrieveUPPER();

private:
    static string firstline[1];
    static string diskeywords[3];
    static char upperword[100];
    static char lowerword[100];
    static char operatorsarr[100];
    static char delimitersarr[100];
 };

这是stack.cpp

#include "stack.h"
#include "stdafx.h"
#include <iostream>


string stack::firstline[1] = { 0 };
string stack::diskeywords[3];

char stack::upperword[100];
char stack::lowerword[100];
char stack::operatorsarr[100];
char stack::delimitersarr[100];

void stack::retrieveUPPER()
{
    for (int i = 0; i < 100; i++)
    {
        cout << upperword[i] << "\n";
    }
}

void stack::push(string readline)
{
    firstline[0] = readline;
    count++;
    check(readline);

}

void stack::check(string readline)
{
    int length;
    char letter;
    int leftperenthe = 0;
    int rightperenthe = 0;
    //int capital;
    //int wordFOR;
    //int wordBEGIN;

    //char keywords[3][8] = { "FOR", "BEGIN", "END" };
    char operators[] = "+-=*/;";
    char delimiters[] = { ',',';' };


    length = readline.length();
    for (int i = 0; i < length; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            letter = readline[i];
            if (isupper(letter)) //if capital letter 
            {
                upperword[i] = letter;
            }
            else if (islower(letter)) //if lowercase letter
            {
                lowerword[i] = letter;
            }
            else if (letter == operators[j]) //if encounters a operator
            {
                operatorsarr[i] = letter;
            }
            else if (letter == delimiters[j]) //if encounters a delimiter
            {
                delimitersarr[i] = letter;
            }
            else if (letter = ' ') //if encounters a space
            {
                lowerword[i] = ' ';
                operatorsarr[i] = ' ';
                delimitersarr[i] = ' ';
            }
            else if (letter = '(') //if left perenthesis
            {
                leftperenthe++;
            }
            else if (letter = ')') //if right perenthesis
            {
                rightperenthe++;
            }
        }
    }
}
visual-c++
2个回答
0
投票

我也有类似的情况:

#include<iostream>
#include<stack.h>
#include<vector>

using namespace std;
int main();
std::vector<int>stickPan (std::vector<int>arr)
{
    stack<int>s;
...
}

我使用了VScode,使用了mingw-w64,并将*.h文件的路径添加到.json文件中,但没有成功。 报告错误:

  1. 标识符“stack”未定义。
  2. 不允许使用类型名称。
  3. 标识符“s”未定义

0
投票

尝试包含 标题,它对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.