12

(初心者のプログラマー..) 正常に機能するヘッダー ファイルのスタイルに従っていますが、コンパイル時にこれらすべてのエラーが発生し続ける方法を理解しようとしています。Cygwin で g++ を使用してコンパイルしています。

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 ‘&lt;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 ‘&lt;anonymous class>’ used as declaration

そして、これが私のクラスヘッダーファイルです...

#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

私はこれらのエラーに混乱しており、クラスの実装に関して何が間違っているのか本当にわかりません..

4

2 に答える 2

29

それは面白いものです。あなたは本質的にあなたのクラス名を殺しています#define Ingredient- の出現はすべてIngredient消去されます。これが、インクルード ガードが一般に の形式をとる理由です#define INGREDIENT_H

また、メンバーとゲッター関数の両方を使用してnameいます (おそらく C# を変換しようとしていますか?)。これは C++ では許可されていません。

于 2013-02-28T06:04:24.223 に答える
4

エラーを見てはどうですか?変数と関数に同じ名前を付けることはできません。また、ガードを含めて、クラスなどの名前を付けないでください。

#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
于 2013-02-28T06:05:20.370 に答える