I'm a beginner to C++ following this tutorial . I'm working on the section dealing with Classes and running into the following error.
not a class or struct name C/C++(262)
I am attempting to inherit the properties of a Shape parent class into a Circle class, but my editor claims that the Shape class is not defined. The error is raised on line 4 in Circle.h
: class Circle: public Shape
. I've included my code below.
I've double checked my code against the code in the tutorial and ensured that all the files I've written are in the same directory, etc.
Any pointers would be much appreciated.
The code editor I'm using is VSCode 1.55.2.
#ifndef SHAPE_H
#define SHAPE_H
class Shape {
protected:
double height;
double width;
static int numOfShapes;
public:
// Constructors
Shape(double length);
Shape(double height, double width);
Shape();
virtual ~Shape();
void SetHeight(double height);
double GetHeight();
void SetWidth(double width);
double GetWidth();
static int GetNumOfShapes();
virtual double Area();
};
#endif /* SHAPE_H */
#include "Shape.h"
// This file is known as the "class implementation" file.
Shape::Shape(double length) {
// '->' is known as the pointer operator (used to access objects fields and methods).
this->height = length;
this->width = length;
}
Shape::Shape(double height, double width) {
this->height = height;
this->width = width;
}
Shape::~Shape() = default; // Sets the deconstructor to the default.
void Shape::SetHeight(double height) {
// You should really use isDigit() on each character to validate the input
this->height = height;
}
double Shape::GetHeight() {return height;}
void Shape::SetWidth(double width)
{
// You should really use isDigit() on each character to validate the input
this->width= width;
}
double Shape::GetWidth() {return width;}
int Shape::GetNumOfShapes() {return numOfShapes;}
double Shape::Area(){
return height * width;
}
int Shape::numOfShapes = 0; // Sets the default value for 'numOfShapes'
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle: public Shape {
public:
// Constructors
Circle();
Circle(const Circle& orig);
Circle(double width);
virtual ~Circle();
double Area();
};
#endif /* CIRCLE_H */
#include "Circle.h"
#include "Shape.h"
#include <cmath>
Circle::Circle(double width): Shape(width) {}
Circle::~Circle() = default;
double Circle::Area() {
return 3.14159 * pow((width/2), 2);
}
#include <cstdlib>
#include <iostream>
#include "Shape.h"
#include "Circle.h"
using namespace std;
void ShowArea(Shape& shape);
int main(int argc, char **argv) {
Shape square(10, 5);
Circle circle(10);
ShowArea(square);
ShowArea(circle);
return 0;
}
void ShowArea(Shape& shape) {
cout << "Area : " << shape.Area() << endl;
}
You are including "Shape.h" after "Circle.h". Hence Circle class is defined before Shape class. The compiler when it reaches line "class Circle: public Shape", it cannot identify "Shape" as any user-defined class or struct, thus it is raising the error. To correct it include "Shape.h" before "Circle.h" or include "Shape.h" in "Circle.h"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.