未知类型名称“类" [英] unknown type name 'class'
问题描述
我正在为几个几何形状创建一个小型库.这样做,我将原型写到shapes.h
文件中,并将方法写到shapes.cpp
文件中.
这是标题:
I'm creating a small library for several geometric shapes. Doing so, I'm writing down prototypes into a shapes.h
file and the methods into a shapes.cpp
file.
This is the header:
#ifndef __shapeslib
#define __shapeslib
class Shape{
protected:
struct dimensions{
double heigth;
double width;
};
double radius; // for circle class to be inherited
public:
Shape(double heigth, double width); // Constructor
Shape(const Shape & shape); // copy constructor for class
~Shape(); // Destructor
virtual double area(double heigth, double width);
virtual double perimeter(double heigth, double width);
void height();
void width();
double rotate(double heigth, double width);
};
但是在Atom软件中保存文件时,出现class Shape{
But when saving the file in Atom software, I get these two errors for the line class Shape{
unknown type name 'class'
expected ';' after top level declarator
我在此处阅读,这可能是因为我使用C而不是C ++进行编译.我真的不知道如何避免这种情况(还是初学者).
I read here that could be because I'm compiling in C rather than C++. I sincerely have no idea about how to avoid this (still a beginner).
我还尝试将文件名从.h
更改为.hpp
,并且似乎可以正常工作.不幸的是,我必须有一个.h
头文件.
I also tried to change the file name from .h
to .hpp
and seems working. Unfortunately, I must have a .h
header file.
任何反馈都非常感谢. 谢谢大家.
Any feedback is really appreciated. Thanks everyone.
推荐答案
实际上,似乎Atom会自动将.h
头文件检测为C语言文件. 此处介绍了解决此问题的几种方法.我尝试使用 ctrl + shift + L 从C到C ++进行手动切换,但是现在没有任何错误了.我可能在单词class
旁边仍然有一个红点,并且显示了这样的错误:
Actually, seems that Atom detects a .h
header file as a C-language file automatically. Several ways to resolve this are explained here. I tried with a manual switch from C to C++ using ctrl+shift+L and now I don't have any error left. I may still have a red point next to the word class
and such an error is showed:
expected ';' after top level declarator
但是代码可以正常运行.
but the code runs normally though.
这篇关于未知类型名称“类"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!