CodeBlocks 告诉我错误:“class”中没有名为“x”的成员

Posted

技术标签:

【中文标题】CodeBlocks 告诉我错误:“class”中没有名为“x”的成员【英文标题】:CodeBlocks is telling me error: no member named 'x' in 'class' 【发布时间】:2021-03-02 22:50:51 【问题描述】:

我目前正在学习 C++ 中的类和对象。我在 CodeBlocks 中用 C++ 编写了源代码和头文件。它们在我的 Mac 上的同一个文件夹中。

当我尝试构建和运行任一文件时,CodeBlocks 告诉我“错误:'class' 中没有名为 'x' 的成员”。

头文件和源代码、错误消息和项目文件夹的屏幕截图如下。如何更正此问题以便源代码文件运行并输出?

LipstickWarehouse2.h

#ifdef LIPSTICKWAREHOUSE2_H_INCLUDED
#define LIPSTICKWAREHOUSE2_H_INCLUDED

using namespace std;

enum Brand

    Mac,
    Glossier,
    EsteeLauder
;

enum Color

    red,
    pink,
    nude,
    black,
    brown,
    gold,
    silver,
    other
;

enum Formula

    matte,
    sheer,
    satin,
    glossy
;

class Lipstick

public:
    Brand Name;
    Color LipColor;
    Color Tube;
    Formula Effect;
    float Price;
    int Rating;
;

void reccomendation(string words)

    if (Rating <= 3)
    
        cout << "I would rethink this lipstick!" << endl;
    
    else
    
        cout << "I would recommend this lipstick!" << endl;
    


#endif // LIPSTICKWAREHOUSE2_H_INCLUDED

ma​​in.cpp

#include <iostream>
#include "LipstickWarehouse2.h"

using namespace std;

int main()

    Lipstick FavBrand;
    FavBrand.Brand = Mac;
    FavBrand.Color = red;
    FavBrand.Formula = matte;
    FavBrand.Tube = black;
    FavBrand.Price = 19.00;
    FavBrand.Rating = 5;
    
    Lipstick WorBrand;
    WorBrand.Brand = Glossier;
    WorBrand.Color = other;
    WorBrand.Formula = sheer;
    WorBrand.Tube = other;
    WorBrand.Price = 18.00;
    WorBrand.Rating = 3;
    
    cout << "Buyer: Should I buy the " << FavBrand.Brand << FavBrand.Color << FavBrand.Formula << "lipstick?" << endl;
    cout << "Seller: " << FavBrand.reccomendation << endl;
    cout << "Buyer: Should I buy the " << WorBrand.Brand << WorBrand.Color << WorBrand.Formula << "lipstick?" << endl;
    cout << "Seller: " << WorBrand.reccomendation << endl;
    return 0;


错误

Line 9: No member named 'Brand' in 'Lipstick'
Line 10: No member named 'Color' in 'Lipstick'
Line 11: No member named 'Formula' in 'Lipstick'
Line 17: No member named 'Brand' in 'Lipstick'
Line 18: No member named 'Color' in 'Lipstick'
Line 19: No member named 'Formula' in 'Lipstick'
Line 24: No member named 'Brand' in 'Lipstick'
Line 24: No member named 'Color' in 'Lipstick'
Line 24: No member named 'Formula' in 'Lipstick'
Line 24: Reference to non-static member function must be called
Line 26: No member named 'Brand' in 'Lipstick'
Line 26: No member named 'Color' in 'Lipstick'
Line 26: No member named 'Formula' in 'Lipstick'
Line 27: Reference to non-static member function must be called

项目文件夹

*编。注意:“.cbp”不是错字。见见original image file)

> bin
LipstickWarehouse2.cbp
LipstickWarehouse2.h
main.cpp
> obj

【问题讨论】:

请将您的代码发布为文本,而不是图像。对于由于某种原因看不到图像的人,您的问题是无法回答的。 作为一般风格指南,如果您的大写区分类型和值,这将很有帮助。因此,如果您的类名和函数以大写字母开头,请考虑让您的变量名至少以小写字母开头。 什么是“.cbp”文件? @Casey CodeBlocks 项目? @PaulSanders 好吧,小字体的 .cpp 和 .cbp 完全不会混淆。 【参考方案1】:

出现错误是因为您尝试按类型(品牌、颜色等)而不是名称(名称、唇色等)访问类成员 像这样更改 main() 中的代码:

Lipstick FavBrand;
FavBrand.Name = Mac;
...

【讨论】:

【参考方案2】:
class Lipstick

public:
    Brand Name;
    Color LipColor;
    Color Tube;
    Formula Effect;
    float Price;
    int Rating;
;

在这里,您声明了一个名为 Name 的成员,其类型为 Brand。要访问它,您需要这样做

    Lipstick FavBrand;
    FavBrand.Name = Mac;

请注意,我们在这里使用Name 而不是Brand,因为Name 是成员的名称,而Brand 是其类型。访问所有其他成员需要使用他们的名字而不是他们的类型。

【讨论】:

以上是关于CodeBlocks 告诉我错误:“class”中没有名为“x”的成员的主要内容,如果未能解决你的问题,请参考以下文章

codeblocks怎么显示编程错误

上codeblocks显示环境错误,怎么办?

CodeBlocks 错误:“文件无法保存”-Win10 64 位-

如何在linux下安装codeblocks

code blocks里无法build。有人告诉我要把gcc设成根目录下的\MinGW。但是我完全没明白啥意思

CodeBlocks环境搭建及创建第一个C++程序