2013-02-28 19 views
8

(Beginner programmer ..) İyi çalışan bir başlık dosyasının stilini takip ediyorum ama derlediğimde bu hataları nasıl aldığımı anlamaya çalışıyorum. Cygwin'de g ++ ile derliyorum.Tam bir sınıf olmak için bir başlık dosyasını düzgün şekilde nasıl kullanırız?

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 

Ve burada ...

#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 

ben bu hatalardan karıştı ve aslına sınıfın uygulanması konusunda yanlış yapıyorum bilmiyorum ..

benim sınıf başlık dosyasıdır
+0

Bu arada, alıcılarınız gayet işe yaramaz, yalnızca her şeyi, parantezleri ve derleyici hatalarını yazma konusunda kendinizi kurtarmanız için herkese açık hale getirin. – Rapptz

cevap

19

Bu eğlenceli bir şey. Sınıf isminizi esasen #define Ingredient ile öldürüyorsunuz - Ingredient'un tüm oluşumları silinecektir. Bu yüzden muhafızlar genellikle #define INGREDIENT_H şeklini alır.

name hem üye hem de alıcı işlevi için (muhtemelen C#? Bu C++ 'ye izin verilmez.

+2

oh aman tanrım, ben de burası dong, sonra bunu okudum ve sadece yüzünü okşadım ... çok teşekkür ederim +1 –

4

Hatalara bakmak nasıl olur? değişkenler ve işlevler aynı ada sahip olamaz. Ve muhafızları asla sınıf gibi bir isim içermemelidir.

#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 
İlgili konular