(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 ..
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