如何正确使用头文件作为完整类? [英] How to properly use a header file to be a complete class?

查看:178
本文介绍了如何正确使用头文件作为完整类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(初学者程序员。。)我遵循的是头文件的样式,可以正常工作,但是我试图弄清楚如何在编译时不断出现所有这些错误。我正在Cygwin中用g ++进行编译。

(Beginner programmer..) I'm following the style of a header file that worked fine, but I'm trying to figure out how I keep getting all of these errors when I compile. I am compiling with g++ in Cygwin.

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

这是我的类头文件...

And here is my class header file...

#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

我对这些错误感到困惑,并不是真的知道我在实现类方面做错了。

I am confused by these errors and don't really know what I am doing wrong concerning the implementation of the class..

推荐答案

这是一个有趣的事情。您实际上是通过 #define成分杀死班级名称的-所有出现的 Ingredient 都将被删除。这就是为什么包含警卫通常采用 #define INGREDIENT_H 的形式。

That's a funny one. You are essentially killing your class name by #define Ingredient - all occurrences of Ingredient will be erased. This is why include guards generally take the form of #define INGREDIENT_H.

您还使用了<$ c $成员和getter函数的c> name (可能是翻译C#的尝试?)。在C ++中是不允许的。

You are also using name both for the member and the getter function (probably an attempt to translate C#?). This is not allowed in C++.

这篇关于如何正确使用头文件作为完整类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆