为什么我的类对象导致未声明的标识符错误?

时间:2019-05-20 22:30:08

标签: c++

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

1 个答案:

答案 0 :(得分:2)

标头中的包含防护是错误的。应该是

#ifndef RIGHTJUST_H_

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

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