好的,所以我的任务是建立在本书的现有代码上.我必须添加第3个框,然后计算所有3个框的总数.这是我到目前为止所写的内容,但它不会编译.请帮我找到问题所在.谢谢.
我正在使用的程序是MS Visual C++,我得到的complile错误是
错误C2447:'{':缺少函数头(旧式正式列表?)
引用{after my 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;
}
Run Code Online (Sandbox Code Playgroud)
更改:
int main();
int Total_Volume;
{
Run Code Online (Sandbox Code Playgroud)
至:
int main()
{
int Total_Volume;
Run Code Online (Sandbox Code Playgroud)
这将解决你的直接问题,虽然我怀疑你今天还会有一些问题:-)
当前代码的实际问题是它为main定义了一个原型,后跟一个文件级变量,后面是一个裸括号,这就是为什么它抱怨缺少函数头.
您可能还需要考虑将您的main功能更改为以下之一:
int main (int argc, char *argv[])
int main (void)
Run Code Online (Sandbox Code Playgroud)
(可能是你的第二个)因为这些是ISO C标准要求支持的两种形式.如果愿意,实现可以自由地接受其他人,但我通常更喜欢我的代码尽可能标准.我说"可能"的原因是因为它不一定需要让你的代码工作,更像是一种风格的东西.
| 归档时间: |
|
| 查看次数: |
608 次 |
| 最近记录: |