2013-02-28 20 views
8

(Beginner programmer ..) Tôi đang theo phong cách của một tập tin tiêu đề mà làm việc tốt, nhưng tôi đang cố gắng tìm ra cách tôi tiếp tục nhận được tất cả các lỗi này khi tôi biên dịch. Tôi đang biên dịch với g ++ trong Cygwin.Cách sử dụng đúng tệp tiêu đề để trở thành một lớp hoàn chỉnh?

Ingredient.h:8:13: error: expected unqualified-id before ‘)’ token 
Ingredient.h:9:25: error: expected ‘)’ before ‘n’ 
Ingredient.h:19:15: error: declaration of ‘std::string <anonymous class>::name’ 
Ingredient.h:12:14: error: conflicts with previous declaration ‘std::string<anonymous class>::name()’ 
Ingredient.h:20:7: error: declaration of ‘int <anonymous class>::quantity’ 
Ingredient.h:13:6: error: conflicts with previous declaration ‘int<anonymous class>::quantity()’ 
Ingredient.h: In member function ‘std::string<anonymous class>::name()’: 
Ingredient.h:12:30: error: conversion from ‘<unresolved overloaded function type>’ to non-scalar type ‘std::string’ requested 
Ingredient.h: In member function ‘int<anonymous class>::quantity()’: 
Ingredient.h:13:25: error: argument of type ‘int (<anonymous class>::)()’ does not match ‘int’ 
Ingredient.h: At global scope: 
Ingredient.h:4:18: error: an anonymous struct cannot have function members 
Ingredient.h:21:2: error: abstract declarator ‘<anonymous class>’ used as declaration 

Và đây là tập tin header lớp học của tôi ...

#ifndef Ingredient 
#define Ingredient 

class Ingredient { 

public: 
    // constructor 
    Ingredient() : name(""), quantity(0) {} 
    Ingredient(std::string n, int q) : name(n), quantity(q) {} 

    // accessors 
    std::string name() { return name; } 
    int quantity() {return quantity; } 

    // modifier 

private: 
    // representation 
    std::string name; 
    int quantity; 
}; 

#endif 

Tôi bối rối bởi các lỗi này và không thực sự biết những gì tôi đang làm sai liên quan đến việc thực hiện của lớp ..

+0

Getters của bạn là khá vô ích bằng cách này, chỉ cần làm cho toàn bộ công chúng để tiết kiệm cho mình những rắc rối của việc viết dấu ngoặc đơn và lỗi trình biên dịch. – Rapptz

Trả lời

19

Điều đó thật thú vị. Về cơ bản bạn đang giết tên lớp của mình theo số #define Ingredient - tất cả các lần xuất hiện của Ingredient sẽ bị xóa. Đây là lý do tại sao bao gồm các vệ sĩ thường có dạng #define INGREDIENT_H.

Bạn cũng đang sử dụng name cho cả thành viên và hàm getter (có thể là một nỗ lực dịch C#?). Điều này không được phép trong C++.

+2

Ôi trời ơi, tôi cũng đã đồng tiền này, sau đó tôi đọc cái này và chỉ phải đối mặt với lòng bàn tay ... cảm ơn bạn rất nhiều +1 –

4

Làm cách nào để tìm lỗi? các biến và hàm không thể có cùng tên. Và bao gồm bảo vệ không bao giờ nên tên như lớp.

#ifndef INGREDIENT_H 
#define INGREDIENT_H 

class Ingredient { 

public: 
    // constructor 
    Ingredient() : name(""), quantity(0) {} 
    Ingredient(std::string n, int q) : name(n), quantity(q) {} 

    // accessors 
    std::string get_name() const { return name; } 
    int get_quantity() const {return quantity; } 

    // modifier 

private: 
    // representation 
    std::string name; 
    int quantity; 
}; 

#endif 
Các vấn đề liên quan