【问题标题】:First C++ Program Getting Errors第一个 C++ 程序出错
【发布时间】:2012-02-06 18:08:35
【问题描述】:

好的,对于一项任务,我们必须制作我们的第一个程序。我的目标是制作一个程序,计算剧院每个座位区的门票收入。问题是我不断收到这个未声明的标识符或标识符未定义的错误以及其他 23 个错误。我使用的程序是 Visual Studio 2010 Premium。

这是我的代码。

// Chp4HWprgm.cpp 
// Created by Bryce Easley on 2/6/2012
#include <iostream>
using namespace std;

int main(){
//declare variables

int orchestraNum = 0;
int mainNum = 0;
int balconyNum =0;
const orchestraPrice = 25;
const mainPrice = 30;
const balconyPrice = 15;

//enter input of sales
cout << "Number of Orchestra tickets sold?";
cin >> orchestraNum;
cout << "Number of Main Floor tickets sold?";
cin >> mainNum;
cout << "Number of Balcony tickets sold?";
cin >> balconyNum;

//calculate revenue for each and total revenue
orchestraTotal = orchestraNum * orchestraPrice;
mainTotal = mainNum * mainPrice;
balconyTotal = balconyNum * balconyPrice;
overallTotal = mainTotal + balconyTotal + orchestraTotal;

//display figures
cout <<"Orchestra Revenue: $" << orchestraTotal << endl;
cout <<"Main Floor Revenue: $" << mainTotal << endl;
cout <<"Balcony Revenue: $" << balconyTotal << endl;
cout <<"Overall Revenue: $" << overallTotal << endl;

system("pause");
return 0;}
//end of main function 

这是我的错误:

错误 6 错误 C2065:'balconyTotal':未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 28 1 Chapter4HW 错误 9 错误 C2065:'balconyTotal':未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 29 1 Chapter4HW 错误 13 错误 C2065:'balconyTotal':未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 34 1 Chapter4HW 错误 5 错误 C2065:'mainTotal':未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 27 1 Chapter4HW 错误 8 错误 C2065:'mainTotal':未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 29 1 Chapter4HW 错误 12 错误 C2065:'mainTotal':未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 33 1 Chapter4HW 错误 4 错误 C2065:'orchestraTotal':未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 26 1 Chapter4HW 错误 10 错误 C2065:'orchestraTotal':未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 29 1 Chapter4HW 错误 11 错误 C2065:'orchestraTotal':未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 32 1 Chapter4HW 错误 7 错误 C2065:'overallTotal':未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 29 1 Chapter4HW 错误 14 错误 C2065:“overallTotal”:未声明 标识符 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 35 1 Chapter4HW 错误 1 ​​错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 默认整数 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 13 1 Chapter4HW 错误 2 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 默认整数 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 14 1 Chapter4HW 错误 3 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 默认整数 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 15 1 Chapter4HW 15 IntelliSense:缺少显式类型('int' 假定)c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 13 7 Chapter4HW 16 IntelliSense:缺少显式类型('int' 假定)c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 14 7 Chapter4HW 17 IntelliSense:缺少显式类型('int' 假定)c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 15 7 Chapter4HW 20 IntelliSense:标识符“balconyTotal”是 未定义 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 28 1 Chapter4HW 19 IntelliSense:标识符“mainTotal”是 未定义 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 27 1 Chapter4HW 18 IntelliSense:标识符“orchestraTotal”是 未定义 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 26 1 Chapter4HW 21 IntelliSense:标识符“overallTotal”是 未定义 c:\users\bryce\desktop\cpp6\chap04\chapter4hw\chapter4hw\chp4hw.cpp 29 1 Chapter4HW

感谢所有回答的人。我一直认真地盯着这个,我想不通。这就像学习一门全新的语言,而且有点混乱!

【问题讨论】:

  • 你没有声明你的总变量...
  • 你得到它是因为你没有声明变量......
  • 并不是说它与您的错误消息有任何关系,但system("pause"); 总是给我——呃,暂停。见stackoverflow.com/q/1107705/10077

标签: c++ compiler-errors


【解决方案1】:

你需要用类型声明你的常量和总变量:

const int orchestraPrice = 25; 
const int mainPrice = 30; 
const int balconyPrice = 15;

...

//calculate revenue for each and total revenue 
int orchestraTotal = orchestraNum * orchestraPrice; 
int mainTotal = mainNum * mainPrice; 
int balconyTotal = balconyNum * balconyPrice; 
int overallTotal = mainTotal + balconyTotal + orchestraTotal;

【讨论】:

  • 常量也需要类型。
  • 如果看到像“auto i”或“const i”这样的变量声明,VC++ 不会添加 unsigned int。它只打印出“Total”变量的错误。
  • 确实如此,但这并不是说你应该让它!
【解决方案2】:

随着您的实践,您很快就会学会理解编译器错误!

const 不是类型,而是修饰符。 orchestraPrice 应该声明:

const int orchestraPrice = 25;

你有三个相似的行有同样的问题。

而且你在使用之前还没有声明orchestraTotal。试试这个:

const int orchestraTotal = orchestraNum * orchestraPrice;

同样,你有三个相似的行有同样的问题。

我建议阅读一两本关于 C++ 的书。见The Definitive C++ Book Guide and List

祝你好运!

【讨论】:

  • 谢谢,我去看看这本书。现在我们正在阅读我的介绍课 An Introduction to Programming with c++ by D.Zak
【解决方案3】:

您需要在使用之前或初始化变量时声明变量。

int orchestraTotal = orchestraNum * orchestraPrice;
int mainTotal = mainNum * mainPrice;
int balconyTotal = balconyNum * balconyPrice;
int overallTotal = mainTotal + balconyTotal + orchestraTotal;

【讨论】:

    【解决方案4】:
    orchestraTotal = orchestraNum * orchestraPrice;
    mainTotal = mainNum * mainPrice;
    balconyTotal = balconyNum * balconyPrice;
    overallTotal = mainTotal + balconyTotal + orchestraTotal;
    

    把这些变成定义,你应该没问题。在使用这些变量之前,您没有声明或定义任何变量,这就是编译器打印错误的原因。

    【讨论】:

      【解决方案5】:

      你还没有声明这 4 个变量:

      mainTotal, bacolnyTotal, orchestraTotal, overallTotal
      

      【讨论】:

      • 此错误的另一个常见原因是拼写和大小写不匹配,您的答案肯定是这样的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多