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''
解决方案
标题中的包含保护是错误的。它应该是
#ifndef RIGHTJUST_H_
如果未定义#ifdef RIGHTJUST_H_
符号,使用将跳过代码。
你不应该#include "stdafx.h"
在你的 .h 文件中。而且由于您似乎使用的是 Visual Studio,因此您可以使用#pragma once
而不是包含防护。
推荐阅读
- android - 如何杀死其他进程或在应用程序中使用 adb 命令行
- compiler-construction - Bison 意外令牌导致语法错误
- javascript - 选择选项的自定义属性并从 Angular 5 中的 AbstractControl 访问它?
- c - 在用 C 编写的自定义 shell 程序中重定向 I/O
- ios - 使用 Siesta 发布 API 时,请求方法返回“无法解析服务器响应”错误
- php - 在 laravel 的 json 响应中获取 HTTP 代码
- python - 无法理解如何使用 lambda 对 dict 进行排序
- regex - 如何从下面给出的数据描述中获取 pandas.DataFrame 的列名
- python - 无法显示图像 HSI 转换为 RGB python
- wordpress - WooCommerce 在结帐电子邮件字段下方添加海关通知