为什么我的类对象会导致未申报的标识符错误

Why is my class object causing undeclared identifier errors?

本文关键字:标识符 错误 我的 对象 为什么      更新时间:2023-10-16

我正在在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''

标题中的包括后卫是错误的。应该是

#ifndef RIGHTJUST_H_

如果符号为未定义,则使用#ifdef RIGHTJUST_H_会跳过代码。

您的.H文件中不应有#include "stdafx.h"。而且,由于您似乎正在使用Visual Studio,因此您可以使用#pragma once而不是Include Guard。