2010-05-05 4 views
0

Windows에서 DevC++의 코드를 컴파일 할 때 잘못된 함수 선언이 나타나는 이유는 무엇입니까?하지만 Linux에서 CodeBlocks로 컴파일하면 제대로 작동합니다.유효하지 않은 함수 선언. DevC++

#include <iostream> 
#include <vector> 


using namespace std; 

//structure to hold item information 
struct item{ 
    string name; 
    double price; 
}; 

//define sandwich, chips, and drink 
struct item sandwich{"Sandwich", 3.00}; **** error is here ***** 
struct item chips{"Chips", 1.50};   **** error is here ***** 
struct item drink{"Large Drink", 2.00}; **** error is here ***** 

vector<item> cart;   //vector to hold the items 
double total = 0.0;   //total 
const double tax = 0.0825; //tax 

//gets item choice from user 
char getChoice(){ 

    cout << "Select an item:" << endl; 
    cout << "S: Sandwich. $3.00" << endl; 
    cout << "C: Chips. $1.50" << endl; 
    cout << "D: Drink. $2.00" << endl; 
    cout << "X: Cancel. Start over" << endl; 
    cout << "T: Total" << endl; 

    char choice; 
    cin >> choice; 
    return choice; 
} 

//displays current items in cart and total 
void displayCart(){ 
    cout << "\nCart:" << endl; 
    for(unsigned int i=0; i<cart.size(); i++){ 
     cout << cart.at(i).name << ". $" << cart.at(i).price << endl; 
    } 
    cout << "Total: $" << total << endl << endl; 
} 

//adds item to the cart 
void addItem(struct item bought){ 
    cart.push_back(bought); 
    total += bought.price; 
    displayCart(); 
} 

//displays the receipt, items, prices, subtotal, taxes, and total 
void displayReceipt(){ 

    cout << "\nReceipt:" << endl; 
    cout << "Items: " << cart.size() << endl; 
    for(unsigned int i=0; i<cart.size(); i++){ 
     cout << (i+1) << ". " << cart.at(i).name << ". $" << cart.at(i).price << endl; 
    } 
    cout << "----------------------------" << endl; 
    cout << "Subtotal: $" << total << endl; 

    double taxes = total*tax; 
    cout << "Tax: $" << taxes << endl; 

    cout << "Total: $" << (total + taxes) << endl; 


} 




int main(){ 

    //sentinel to stop the loop 
    bool stop = false; 
    char choice; 
    while (stop == false){ 

     choice = getChoice(); 

     //add sandwich 
     if(choice == 's' || choice == 'S'){ 
      addItem(sandwich); 
     } 
     //add chips 
     else if(choice == 'c' || choice == 'C'){ 
      addItem(chips); 
     } 
     //add drink 
     else if(choice == 'd' || choice == 'D'){ 
      addItem(drink); 
     } 
     //remove everything from cart 
     else if(choice == 'x' || choice == 'X'){ 
      cart.clear(); 
      total = 0.0; 
      cout << "\n***** Transcation Canceled *****\n" << endl; 
     } 
     //calcualte total 
     else if(choice == 't' || choice == 'T'){ 
      displayReceipt(); 
      stop = true; 
     } 
     //or wront item picked 
     else{ 
      cout << choice << " is not a valid choice. Try again\n" << endl; 
     } 

    }//end while loop 


    return 0; 
    //end of program 
} 

답변

1

당신이 할당 연산자를 놓치고 : 이것은 C 구문하지만 것을

struct item sandwich = {"Sandwich", 3.00}; 

참고. 당신은 아마

item sandwich("Sandwich", 3.00); 

말과 itemstringdouble를 사용하는 생성자를 추가 할.

1

struct item sandwich{"Sandwich", 3.00};compound literal의 사용법이며 C (C99 표준)에서는 적합하지만 C++에서는 적합하지 않습니다. 그러나 대부분의 C++ 컴파일러는 C 및 C++ 코드를 모두 컴파일하므로 일부는 C++에서 이러한 구조를 허용하기로 결정합니다. 대부분은 특별한 커맨드 라인 인수 없이는하지 않습니다.

이렇게 합법적이고 이식 가능하려면 항목 구조체에 대한 생성자를 작성해야합니다. 당신이

item sandwich("Sandwich", 3.00); 

P.S.에 새 항목을 만들 수 있습니다

struct item { 
    item(string const & name_, double price_) : name(name_), price(price_) {} 
    string name; 
    double price; 
}; 

하지만 이제이 쉽다 단일 구조에서 다른 의미를 가진 필드가있을 때 복합 리터럴에서 이름이 지정된 초기화 프로그램을 사용한다는 점에 유의하십시오. 그 때 무엇이 ​​무엇인지 쉽게 이해할 수 있습니다.

struct item sandwich = {.name = "Sandwich", .price = 3.0}; 

물론 C에서는 잘 작동하지 않지만 물론 좋을 것 같습니다.

P.P.S. 밝혀졌습니다. C++ 0x에 충분한주의를 기울이지 않았고, 거기에 initializer lists이라고합니다. 당신이 그것을 지명 할 수없는 것처럼 보입니다, 그것은 수치입니다. 그래서 C++ 코드에서 C99 표준을 사용하는 대신 Linux 컴파일러는 C++ 0x 실험 표준을 자동으로 사용했습니다. 그럼에도 불구하고 크로스 플랫폼 코드를 원한다면 멋진 기능에서 벗어나 일반 오래된 생성자를 대신 사용하는 것이 좋습니다.

+0

LOL _ "효과가 없지만 적어도 좋아 보인다" – wilhelmtell

0

Dev-C++은 이전 버전의 MinGW 컴파일러를 사용합니다.

: C++ 0X (GCC의 구체적 4.4 시리즈)의 확장 초기화리스트 기능을 지원하는는 MinGW 프로젝트에서 GCC의 최신 버전을 사용하면 오류로 표시된 라인에 대한 경고와 불평한다 testing.cc:14 : 경고 : 확장 이니셜 라이저 목록은 -std = C++ 0x 또는 -std = gnu ++ 0x testing.cc:15 : 경고 : 확장 이니셜 라이저 목록은 과 함께 사용 가능합니다. -std = C++ 0x 또는 -std = gnu ++ 0x testing.cc:16 : 경고 : 확장 이니셜 라이저 목록은 -std = C++ 0x 또는 -std = gnu ++ 0x

gcc 4.4.3의 제 버전이 어쨌든 불만을 토로합니다 ... 귀하의 정보가 확실하지 않습니다.