首页 > 解决方案 > 为什么我的类对象会导致未声明的标识符错误?

问题描述

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

标签: c++

解决方案


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

#ifndef RIGHTJUST_H_

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

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


推荐阅读