0

I am currently learning about classes and objects in C++. I have written a source code and a header file in C++ in CodeBlocks. They are in the same folder on my Mac.

When I try to build and run either file, CodeBlocks tells me "error: no member named 'x' in 'class'".

A screenshot of the header file and source code, the error messages, and the project folder are below. How can I correct this so that the source code file runs and outputs?

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

main.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;
}

Errors

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

Project folder

*Ed. Note: ".cbp" is not a typo. See See original image file)

> bin
LipstickWarehouse2.cbp
LipstickWarehouse2.h
main.cpp
> obj
7
  • 1
    Please post your code as text, not images. For people that can't see images for one reason or another your question is unanswerable. Commented Mar 2, 2021 at 22:53
  • As a general style guide, it's helpful if your capitalization distinguishes between types and values. So, if your class names and functions begin with a capital letter, consider making your variable names begin with a lowercase letter at the very least. Commented Mar 2, 2021 at 23:04
  • What the heck is a ".cbp" file? Commented Mar 2, 2021 at 23:14
  • @Casey CodeBlocks Project? Commented Mar 2, 2021 at 23:18
  • @PaulSanders Well that's not going to be confusing at all between .cpp and .cbp in a tiny font. Commented Mar 2, 2021 at 23:19

2 Answers 2

1

The error appears because you're trying to access class members by their types (Brand, Color, etc.) instead of names (Name, LipColor, ...)
Change your code in main() like this:

Lipstick FavBrand;
FavBrand.Name = Mac;
...
Sign up to request clarification or add additional context in comments.

Comments

1
class Lipstick
{
public:
    Brand Name;
    Color LipColor;
    Color Tube;
    Formula Effect;
    float Price;
    int Rating;
};

Here you declare a member named Name which has type Brand. To access this, you need to do

    Lipstick FavBrand;
    FavBrand.Name = Mac;

Notice that we use Name here instead of Brand because Name is the name of the member whereas Brand is its type. Accessing all other members needs to use their names rather than their types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.