2010-02-28 17 views
1

好的,所以我的任務是建立在本書現有的代碼上。我必須添加第三個框,然後計算所有3個框的總數。這是我到目前爲止所寫的內容,但它不會被編譯。請幫我找到問題。謝謝。我的代碼有什麼問題?我的程序不會編譯

我正在使用的程序是微軟的Visual C++和請編譯錯誤我得到的是

錯誤C2447: '{':缺少函數頭(舊式正式列表?)

指{我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; 
} 
+0

請格式化你的代碼。 – 2010-02-28 22:47:46

回答

8

變化:

int main(); 
int Total_Volume; 
{ 

到:

int main() 
{ 
    int Total_Volume; 

這將能夠立即解決的問題,但我懷疑你今天就會有一些問題:-)

的實際問題與您當前的代碼,它定義一個main的原型,接着是一個文件級變量,接着是一個裸括號,這就是爲什麼它抱怨缺少函數頭。

可能還需要考慮改變你main功能之一:

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

(可能是第二次在你的情況下),因爲這些都需要由ISO C標準所支持的兩種形式。如果他們願意,實現可以自由地接受其他人,但我通常更喜歡我的代碼儘可能標準化。我說「可能」的原因是因爲它不一定需要讓你的代碼工作,更多的是風格的東西。

+0

真棒!這有助於我自己完成剩下的工作。感謝paxdiablo! – Russton 2010-02-28 23:01:33

+0

@Russton - 我會很好,並將其標記爲接受的答案。它給paxdiablo一些代表,並顯示問題有一個答案可供具有類似查詢的人使用。 – Edd 2010-03-01 13:48:46

3
int main(); 
int Total_Volume; 
{ 

應該

int main() 
{ 
    int Total_Volume; 
0

嘗試{

+0

這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 – bitmask 2012-08-24 15:09:43

+0

@bitmask:請再次閱讀我的答案。它爲這個問題提供了一個答案。事實上,它包含與其他4個答案完全相同的建議,所有答案都在一分鐘內提交。很明顯,我沒有贏得[FGIW](http://meta.stackexchange.com/questions/9731/fastest-gun-in-west-problem),但這不是一個批評或要求澄清。 – Oddthinking 2012-08-24 16:36:43

+0

也許我很挑剔,但它建議嘗試一些東西,而不是說明什麼是錯的。除此之外;你的回答實際上是錯誤的,因爲在''''''''''''''''''後面留下分號仍然不能編譯的聲明。誠然,答案時間似乎是相同的所有職位,但這個答案只是增加了噪音,因爲它不增加其他更詳細的答案也沒有提供的細節(你不會鬆動代表,順便說一句。 )。 – bitmask 2012-08-24 16:49:10

0

天哪後移動

int Total_Volume; 

。請檢查預覽,並至少確保您的問題看起來像散文加一些代碼。 編輯好吧,我們假設這是一個已經解決的故障。我的答案的其餘部分仍然成立。

有點挖掘表明第一個問題是你沒有一個正確定義的main()函數。這是人們通常首先了解C++的第一件事情,所以首先得到正確的答案。

祝你好運。

0

你可能打算寫:

int main() 
{ 
    int TotalVolume; 

無法啓動,你做了一個塊。 你已經聲明將會有一個函數main(),並且有一個全局變量TotalVolume,但是不允許在這之後的匿名塊。

1

看起來你混了幾行:

int main(); 
int Total_Volume; 
{ 
CBox box1; 
CBox box2; 
CBox box3; 

看起來應該是:

int main() { 
    int Total_Volume; 
    CBox box1; 
    CBox box2; 
    CBox box3;