[英]What is wrong with my code? My program will not compile
Ok, so my assignment is to build on an existing code from the book. 好的,所以我的任务是基于本书的现有代码。 I have to add the 3rd box and then calculate the total for all 3 boxes.
我必须添加第3个框,然后计算所有3个框的总数。 Here is what I've written so far, but it will not compile.
这是我到目前为止编写的内容,但是不会编译。 Please help me find the problem.
请帮我发现问题。 Thanks.
谢谢。
The program I'm using is MS Visual C++ and the complile error I get is 我正在使用的程序是MS Visual C ++,并且遇到的错误是
error C2447: '{' : missing function header (old-style formal list?)
错误C2447:“ {”:缺少函数头(旧式的正式列表?)
referring to the { after my int Total_Volume line 指的是{在我的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;
}
Change: 更改:
int main();
int Total_Volume;
{
to: 至:
int main()
{
int Total_Volume;
That will fix your immediate problem, although I suspect you'll have a few more questions today :-) 这将解决您的直接问题,尽管我怀疑您今天还会有其他问题:-)
The actual problem with your current code is that it defines a prototype for main, followed by a file-level variable, followed by a naked brace, which is why it's complaining about a missing function header. 当前代码的实际问题是,它为main定义了一个原型,然后是文件级变量,然后是一个裸括号,这就是为什么它抱怨缺少函数头。
You may also want to consider changing your main
function to one of: 您可能还需要考虑将
main
功能更改为以下一项:
int main (int argc, char *argv[])
int main (void)
(probably the second in your case) as these are the two forms required to be supported by the ISO C standard. (您的情况下可能是第二种),因为这是ISO C标准要求支持的两种形式。 Implementations are free to accept others if they wish but I usually prefer my code to be as standard as possible.
如果愿意,实现可以免费接受其他人,但是我通常希望我的代码尽可能地标准。 The reason I say "may" is because it's not necessarily required to get your code working, more of a style thing.
我之所以说“可能”,是因为不一定要使您的代码正常工作,更像是一种样式。
int main();
int Total_Volume;
{
should be 应该
int main()
{
int Total_Volume;
It looks like you mixed up a couple lines: 您好像混了几行:
int main();
int Total_Volume;
{
CBox box1;
CBox box2;
CBox box3;
looks like it should be: 看起来应该是:
int main() {
int Total_Volume;
CBox box1;
CBox box2;
CBox box3;
Try moving 尝试移动
int Total_Volume;
after the { 之后 {
Goodness me. 天啊。 Please check the preview and at least make sure your question looks like prose plus some code.
请检查预览,并至少确保您的问题看起来像散文,外加一些代码。 Edit okay, let's assume that was a glitch that's been sorted out.
编辑好吧,我们假设这是一个已解决的故障。 The rest of my answer still holds.
我的其余答案仍然成立。
A little bit of digging suggests the first problem is that you don't have a properly defined main() function. 进行一点挖掘表明第一个问题是您没有正确定义的main()函数。 This is the first thing one learns about C++ typically, so in the first instance get that right.
这是通常学习C ++的第一件事,因此首先要弄清这一点。
Good luck. 祝好运。
You probably intended to write: 您可能打算写:
int main()
{
int TotalVolume;
You can't start a block where you did. 您无法从那里开始一个程序块。 You have declared that there will be a function
main()
, and that there's a global variable TotalVolume, but the anonymous block following these is not allowed. 您已经声明将有一个函数
main()
,并且有一个全局变量TotalVolume,但是后面的匿名块是不允许的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.