Okay, so I've got a carpet calculator program. It figures feet, calculates cost. I have a class named RoomCarpet that I receive the error C2228 left of '.getFeet' must have a class/struct/union in the .cpp file. I'm declaring in RoomCarpet.h and trying to implement in RoomCarpet.cpp.

I have the .cpp file here:

#include "RoomCarpet.h"

// Function to return total cost
double RoomCarpet::getTotalCost(RoomDimension size, double cost)
{
    FeetInches width;
    FeetInches length;
    return size.getArea(width,length).getFeet() * cost;
}

and the .h file here

//RoomCarpet.h

#ifndef ROOMCARPET_H
#define ROOMCARPET_H
#include "RoomDimension.h"


class RoomCarpet
{


    public:
        double cost;  // To hold cost per square foot
        RoomDimension size; // RoomDimension attribute
        RoomCarpet();
        RoomCarpet(RoomDimension s, double cost)
        {
            this->cost = cost;
            size = s;
        }

        // Funtion to return total cost
        double getTotalCost(RoomDimension s, double cost);
};
#endif

In most questions regarding this error, I have found that a missing semicolon or perhaps the use of () caused this. I have removed the () from getFeet to attempt a fix, same error. Instead of a . I have tried -> because that was also suggested in a different forum, this caused many more errors - one specifically is expression must have a pointer type. Any ideas on this? Any help is greatly appreciated. I have the FeetInches class available as well if needed.

return size.getArea(width,length).getFeet() * cost;

Let's take a look at this. So, you've got size, which is an object of type RoomDimension. And you're calling the class function getArea on it, like this: size.getArea(width,length). And that class function, getArea(), will be returning something. What kind of object does it return?

Because whatever it returns, you're trying to call the class function getFeet() on it, and your compiler is telling you that whatever that object is, it is not a class that has a getFeet function. So what kind of object does getArea() return, and why do you think it has a class function getFeet?

WHile I'm here, you're using width and length, but you didn't ever set them to any meaningful values. So they're just random values. Shouldn't you be getting the width and the length from the Roomdimension object? That said, the whole point of having proper objects is so that they handle this sort of thing themselves, and a well made Roomdimension object would have a function getArea so you didn't have to bother calculating it youself.

I figured it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.