2013-02-28 8 views
8

(Programmatore principiante ..) Sto seguendo lo stile di un file di intestazione che ha funzionato bene, ma sto cercando di capire come continuo a ottenere tutti questi errori quando compilo. Sto compilando con g ++ in Cygwin.Come utilizzare correttamente un file di intestazione per essere una classe completa?

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 

E qui è il mio file di intestazione di classe ...

#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 

Sono confuso da questi errori e non so davvero che cosa sto facendo male, relativa alla implementazione della classe ..

+0

I tuoi getter sono piuttosto inutili a proposito, basta rendere l'intera cosa pubblica per risparmiarti la fatica di scrivere le parentesi e gli errori del compilatore. – Rapptz

risposta

19

È divertente. Stai essenzialmente uccidendo il nome della tua classe entro #define Ingredient - tutte le occorrenze di Ingredient verranno cancellate. Questo è il motivo per cui le guardie generalmente assumono la forma di #define INGREDIENT_H.

Si sta utilizzando anche name sia per il membro che per la funzione getter (probabilmente un tentativo di tradurre C#?). Questo non è permesso in C++.

+2

oh mio Dio, sono stato anche questo dong, poi ho letto questo e ho appena fatto una smorfia ... grazie mille +1 –

4

E che dire degli errori? variabili e funzioni non possono avere lo stesso nome. E includere la guardia non dovrebbe mai nomi come la classe.

#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 
Problemi correlati