c++ - 我的代码有什么问题?我的程序无法编译

标签 c++

好的,所以我的任务是根据书中的现有代码进行构建。我必须添加第 3 个框,然后计算所有 3 个框的总数。这是我到目前为止所写的内容,但无法编译。请帮我找出问题所在。谢谢。

我正在使用的程序是 MS Visual C++,我得到的编译错误是

error C2447: '{' : missing function header (old-style formal list?)

指的是 { 在我的 int Total_Volume 行之后

// Structures_and_classes.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
class CBox // Class definition at global scope
{
public:
 double m_Length; // Length of a box in inches
 double m_Width; // Width of a box in inches
 double m_Height; // Height of a box in inches
};
int main();
int Total_Volume;
{
CBox box1;
CBox box2;
CBox box3;
 double boxVolume = 0.0;             // Stores the volume of a box
  box1.m_Height = 18.0;            // Define the values
  box1.m_Length = 78.0;             // of the members of
  box1.m_Width = 24.0;              // the object box1
  box2.m_Height = box1.m_Height - 10;         // Define box2     Box 2 H = 8
  box2.m_Length = box1.m_Length/2.0;          // members in    Box 2 L = 39
  box2.m_Width = 0.25*box1.m_Length;           // terms of box1  Box 2 W = 6
  box3.m_Height = box1.m_Height + 2;         // Define box3     Box 3 H = 20
  box3.m_Length = box1.m_Length - 18;          //members in    Box 3 L = 50
  box3.m_Width = box1.m_Width + 1;           //terms of box1   Box 3 W = 25 

  // Box1
  boxVolume = box1.m_Height*box1.m_Length*box1.m_Width;cout << endl;
<< "Volume of box1 = " << boxVolume;
  cout << endl;
  // Box 2
  boxVolume = box2.m_Height*box2.m_Length*box2.m_Width;cout << endl;
<< "Volume of box2 = " << boxVolume;
  cout << endl;
  // Box 3
  boxVolume = box3.m_Height*box3.m_Length*box3.m_Width;cout << endl;
<< "Volume of box3 = " << boxVolume;
  cout << endl;

//Calculate Total Volume
  Total_Volume = (box1.m_Height*box1.m_Length*box1.m_Width)+
      (box2.m_Height*box2.m_Length*box2.m_Width)+
      (box3.m_Height*box3.m_Length*box3.m_Width);

return 0;
}

最佳答案

改变:

int main();
int Total_Volume;
{

到:

int main()
{
    int Total_Volume;

这将解决您眼前的问题,尽管我怀疑您今天会有更多问题:-)

您当前代码的实际问题是它为 main 定义了一个原型(prototype),后跟一个文件级变量,然后是一个裸括号,这就是它提示缺少函数头的原因。

可能还想考虑将您的main 函数更改为以下之一:

int main (int argc, char *argv[])
int main (void)

(在您的情况下可能是第二种),因为这是 ISO C 标准需要支持的两种形式。如果他们愿意,实现可以自由接受其他人,但我通常更希望我的代码尽可能标准。我之所以说“可能”,是因为它不一定是让您的代码正常工作所必需的,更多的是一种风格。

关于c++ - 我的代码有什么问题?我的程序无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2353004/

相关文章:

c++ - SDL2 在 Windows 上没有控制台输出

c++ - 在这个 Singleton 习语中,我们真的必须让 child class 成为 friend 吗?

c++ - 位域的内存位置

c++ - 括号后跟括号

c++ - 编译器通过错误:尝试使用 std::pair 的 vector 时没有匹配函数

c++ - 释放动态内存

c++ - 错误: non-lvalue in assignment

c++ - 拼接后裁剪图像

c++ - 对同一原子变量混合放松访问和获取/释放访问如何影响同步?

c++ - 为什么 clang 缺少参数包错误的默认参数?