【问题标题】:Why is my class object causing undeclared identifier errors?为什么我的类对象会导致未声明的标识符错误?
【发布时间】:2019-10-07 07:08:03
【问题描述】:

我正在 Microsoft Visual C++ 2010 Express 上编写程序,但我的类对象存在问题。我不断收到未声明的标识符并且缺少';'在标识符错误之前,我不确定是什么导致了问题。

我检查了我的标题是否有任何错误,并进行了一些更改,看看它是否能解决问题。

// RightJust class declaration that right justifies the numbers.
#ifdef RIGHTJUST_H_
#define RIGHTJUST_H_
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace System;
using namespace std;
class RightJust
{
    private:
        int x, totalWH1, totalWH2, totalWH3, totalWH4;  
        int itemT1, itemT2, itemT3, itemT4, itemT5;     // Holds item totals
        string WH1, WH2, WH3, WH4;
        int num[4][5];          // Array containing data
    public:
        RightJust();
        int warehouseLen();
        void rightJustZero();
        void rightJustLess();

};
// This program asks to user to choose a report type and displays
// the report type chosen.

#include "stdafx.h"
#include <iostream>
#include "RightJust.h"
#include <string>
#include <cstring>
using namespace System;
using namespace std;

int main
{
       RightJust type;
    if(reportType == 1)
    {
        type.rightJustZero();
    }
    else if(reportType == 2)
    {
        type.rightJustLess();
    }
}

我只是想要一个解决方案来消除所有错误消息,以查看我的程序是否按照我需要的方式运行。

Test3.cpp
1>Test3.cpp(24): error C2065: 'RightJust' : undeclared identifier
1>Test3.cpp(24): error C2146: syntax error : missing ';' before identifier 'type'
1>Test3.cpp(24): error C2065: 'type' : undeclared identifier
1>Test3.cpp(27): error C2065: 'type' : undeclared identifier
1>Test3.cpp(27): error C2228: left of '.rightJustZero' must have class/struct/union
1>          type is ''unknown-type''
1>Test3.cpp(31): error C2065: 'type' : undeclared identifier
1>Test3.cpp(31): error C2228: left of '.rightJustLess' must have class/struct/union
1>          type is ''unknown-type''

【问题讨论】:

  • 标题中缺少#endif
  • 我不确定这些代码是否足以解决我的问题。如果它重现您的错误消息就足够了。
  • 永远不要将#include "stdafx.h" 放在标题中。
  • 欢迎来到 SO。阅读 How to Askminimal reproducible example 可能会有所帮助——MCVE 解释了你是如何知道这一点的:我不确定这是否足以找到解决我问题的代码

标签: c++


【解决方案1】:

标题中的包含保护错误。应该是

#ifndef RIGHTJUST_H_

如果符号定义,使用#ifdef RIGHTJUST_H_将跳过代码。

您的 .h 文件中不应包含 #include "stdafx.h"。而且由于您似乎使用的是 Visual Studio,因此您可以使用 #pragma once 而不是包含保护。

【讨论】:

  • 我已经进行了更改,但现在我在构造函数中初始化的二维数组出现错误。 num[4][5] ={14,15,5,0,16,15,20,25,0,5,25,25,40,30,20,5,3,10,0,10};error C2059: syntax error : '{'
  • @ymneedhelp 你应该问一个不同的问题。这与此完全无关。编辑这应该有帮助:stackoverflow.com/a/4981220/487892
猜你喜欢
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
相关资源
最近更新 更多