2016-03-08 86 views
0

我想爲我的C++類做一個任務,並且卡住了。我跟蹤了這​​本書,並在互聯網上尋找答案,但沒有達到答案。我需要在一個結構內部有一個數組,它會向客戶顯示一個含有價格的早餐菜單。他們會選擇他們想要的東西,當他們完成時,它會顯示他們的賬單。這是迄今爲止我所知道的,這並不是因爲錯誤。結構,數組錯誤C2512

#include "stdafx.h" 
#include <iomanip> 
#include <iostream> 
#include <string> 


using namespace std; 


int main() 
{ 
cout << fixed << setprecision(2); 

const string menuList[9] 
{ "Plain Egg", "Bacon and Egg", "Muffin", "Frech Toast", "Fruit Basket",  "Cereal", "Coffee", "Tea" }; 

double prices[9] 
{1.45, 2.45, 0.99, 1.99, 2.49, 0.69, 0.50, 0.75}; 

struct menuItemType 
{ 
    const string menuList[9]; 
    double price; 
    double tax; 
}; 

menuItemType guestList; 

system("pause"); 
return 0; 
} 
+0

請在你的問題中逐字添加錯誤信息。對於非MVC編譯器,_C2512_是非常沒有意義的。 –

+0

錯誤C2512:'main :: menuItemType';沒有適當的默認構造函數可用 – Raj

+0

'menuItemType :: menuList'是'const',所以你如何期待初始化它而不給'menuItemType'一個顯式的構造函數? – jamesdlin

回答

0

你應該聲明你的結構在main之上。然後稱之爲主

menuItemType* guestlist = new menuItemType(); 

從我所看到的這應該有所幫助。

+0

您正在將'new'內存分配給非指針(這既不是Java也不是C#) – crashmstr

+0

這是一個排字錯誤 –

+1

謝謝你的回答,它有幫助。我在main之前移動了結構,然後使用:menuItemType guestList();並修復它。 – Raj