Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误:‘;气缸’;未在此范围中声明 我在C++中用C++编写代码,接收:_C++_Xcode - Fatal编程技术网

错误:‘;气缸’;未在此范围中声明 我在C++中用C++编写代码,接收:

错误:‘;气缸’;未在此范围中声明 我在C++中用C++编写代码,接收:,c++,xcode,C++,Xcode,错误:“圆柱体”未在此范围内声明 头文件柱面.h: #include <iostream> #ifndef cylinder_h #define cylinder_h #endif #include "stdio.h" using namespace std; class cylinder { public: // Constructors cylinder(); cylinder(double r, double h); // Acce

错误:“圆柱体”未在此范围内声明

头文件柱面.h:

#include <iostream>
#ifndef cylinder_h
#define cylinder_h
#endif

#include "stdio.h"

using namespace std;

class cylinder
{


public:
    // Constructors

    cylinder();
    cylinder(double r, double h);

    // Accessors
    double getRadius();
    double getHeight();
    void setRadius(double r);
    void setHeight(double h);
    double area();
    double volume();
   void write(std::ostream& output);

private:
    double radius;
    double height;

};
#include <iostream>
#ifndef cylinder_h
#define cylinder_h
#endif

#include "stdio.h"

using namespace std;

class cylinder
{


public:
    // Constructors

    cylinder();
    cylinder(double r, double h);

    // Accessors
    double getRadius();
    double getHeight();
    void setRadius(double r);
    void setHeight(double h);
    double area();
    double volume();
   void write(std::ostream& output);

private:
    double radius;
    double height;

};
#ifndef cylinder_h
#define cylinder_h

#include <iostream>

class cylinder
{

public:
    // Constructors
    cylinder();
    cylinder(double r, double h);

    // Accessors
    double getRadius();
    double getHeight();
    void setRadius(double r);
    void setHeight(double h);
    double area();
    double volume();
    void write(std::ostream& output);

private:
    double radius;
    double height;

};
#endif
main.cpp:

#include <iostream>
using namespace std;
#include <string>
#include "cylinder.h"
#include <iomanip>
#include "sphere.h"
#include "prism.h"
#include "cone.h"
#include "pyramid.h"
#include <cstdlib>



int main()
{
    double radius, height,sradius,length,width,rheight,cheight,cradius,pheight,plength;



    cout << "Enter cylinder height and radius >>> ";
    cin >> height >> radius;
    cylinder one (radius, height);
    cout << "The cylinder volume is " << setprecision(2)<<fixed<<one.volume () << endl;
    cout << "The cylinder surface area is " << setprecision(2)<<fixed<<one.area () << endl;
    cout <<"CYLINDER: "<<height<<", "<<radius<<endl;
}
#include <iostream>
using namespace std;
#include <string>
#include "cylinder.h"
#include <iomanip>
#include <cstdlib>

int main()
{
    double radius, height,sradius,length,width,rheight,cheight,cradius,pheight,plength;



    cout << "Enter cylinder height and radius >>> ";
    cin >> height >> radius;
    cylinder one (radius, height);
    cout << "The cylinder volume is " << setprecision(2)<<fixed<<one.volume () << endl;
    cout << "The cylinder surface area is " << setprecision(2)<<fixed<<one.area () << endl;
    cout <<"CYLINDER: "<<height<<", "<<radius<<endl;
}
#include <iostream>
#include <string>
#include <iomanip>

#include "cylinder.h"

using namespace std;

int main()
{
    double radius, height;



    cout << "Enter cylinder height and radius >>> ";
    cin >> height >> radius;
    cylinder one(radius, height);
    cout << "The cylinder volume is " << setprecision(2) << fixed << one.volume() << endl;
    cout << "The cylinder surface area is " << setprecision(2) << fixed << one.area() << endl;
    cout << "CYLINDER: " << height << ", " << radius << endl;
}
#包括
使用名称空间std;
#包括
#包括“汽缸.h”
#包括
#包括“sphere.h”
#包括“prism.h”
#包括“cone.h”
#包括“pyramid.h”
#包括
int main()
{
双半径、高度、sradius、长度、宽度、rheight、cheight、cradius、pheight、plength;
cout>高度>>半径;
气缸1(半径、高度);

不能试着把#endif放在头文件的末尾

我想这是因为你的多次“使用名称空间std”,只使用头文件中的一个,并且一定要把它放在后面#包括一种试图找到答案而不是答案的方法。它已作为答案发布,因为我认为我无法将其全部放在评论的范围内

我已经删除了未提供的内容,更正了包含守卫的用法,修改了一些内容,并注释掉了不需要的内容。希望我已经对我所做的事情和原因留下了足够好的解释。如果没有,请询问

这是编译的。我还没有测试逻辑

如何处理它:

在main.cpp中包含了一堆文件,但没有提供。为了获得一个工作基础,我将它们注释掉。添加它们并逐个重建程序,直到程序停止编译。如果这并没有使问题变得明显,至少减少了搜索区域

修改后的气缸.h

// two lines below are an include guard. It prevents a header file from being included
// multiple times, heading off potentially recursive includes (a loop that
// causes the compiler to go forever) and chaos caused by redefining the same
// stuff multiple times.
#ifndef cylinder_h
#define cylinder_h

#include <iostream>
//#include "stdio.h" unused and should be #include <cstdio> when used in C++

//using namespace std; unused and very dangerous.

class cylinder
{
public:
    // Constructors

    cylinder();
    cylinder(double r, double h);

    // Accessors
    double getRadius();
    double getHeight();
    void setRadius(double r);
    void setHeight(double h);
    double area();
    double volume();
   void write(std::ostream& output);

private:
    double radius;
    double height;

};
#endif // end of include guard moved to here
修订的main.cpp

#include <iostream>
//#include <string> not needed
#include "cylinder.h"
#include <iomanip>

// the following headers were not provided and may be containing bad code that 
// breaks the OP's build. No way to tell. Add one and rebuild. If the program still 
//compiles, the problem is likely elsewhere so add another and rebuild.
//#include "sphere.h"
//#include "prism.h"
//#include "cone.h"
//#include "pyramid.h"

//#include <cstdlib> not used

//using namespace std; used but use with caution. Instead, use only the pieces you need
using std::cout;
using std::cin;
using std::setprecision;
using std::fixed;
using std::endl;

// or explicitly state the namespace at each use. 
// Eg. std::cout << "blah blah blah" << std::endl; 


int main()
{
    double radius, height;//,sradius,length,width,rheight,cheight,cradius,pheight,plength;



    cout << "Enter cylinder height and radius >>> ";
    cin >> height >> radius;
    cylinder one (radius, height);
    cout << "The cylinder volume is " << setprecision(2)<<fixed<<one.volume () << endl;
    cout << "The cylinder surface area is " << setprecision(2)<<fixed<<one.area () << endl;
    cout <<"CYLINDER: "<<height<<", "<<radius<<endl;
}
#包括
//#包括不需要的
#包括“汽缸.h”
#包括
//未提供以下标头,可能包含以下错误代码:
//破坏OP的构建。无法判断。添加一个并重建。如果程序仍然
//编译时,其他地方可能会出现问题,因此请添加另一个并重新生成。
//#包括“sphere.h”
//#包括“prism.h”
//#包括“cone.h”
//#包括“pyramid.h”
//#包括未使用
//使用名称空间std;已使用,但请谨慎使用。相反,请仅使用所需的部分
使用std::cout;
使用std::cin;
使用std::setprecision;
使用std::fixed;
使用std::endl;
//或者在每次使用时显式声明名称空间。
//例如,标准::曲线>高度>>半径;
气缸1(半径、高度);

cout这是在我的gnu/linux系统上编译和运行的原始代码。我尽量不做太多更改。我认为将旧代码与新代码进行比较,看看最小的更改是什么,这会让您受益匪浅

在那之后,我展示了一个更干净的代码版本,我也没有真正纠正它,特别是使用的样式和功能,但我只是尝试删去了很多不必要的东西。我认为意识到不需要的东西也可以为你解决一些问题

更改最少的代码:

气缸h:

#include <iostream>
#ifndef cylinder_h
#define cylinder_h
#endif

#include "stdio.h"

using namespace std;

class cylinder
{


public:
    // Constructors

    cylinder();
    cylinder(double r, double h);

    // Accessors
    double getRadius();
    double getHeight();
    void setRadius(double r);
    void setHeight(double h);
    double area();
    double volume();
   void write(std::ostream& output);

private:
    double radius;
    double height;

};
#include <iostream>
#ifndef cylinder_h
#define cylinder_h
#endif

#include "stdio.h"

using namespace std;

class cylinder
{


public:
    // Constructors

    cylinder();
    cylinder(double r, double h);

    // Accessors
    double getRadius();
    double getHeight();
    void setRadius(double r);
    void setHeight(double h);
    double area();
    double volume();
   void write(std::ostream& output);

private:
    double radius;
    double height;

};
#ifndef cylinder_h
#define cylinder_h

#include <iostream>

class cylinder
{

public:
    // Constructors
    cylinder();
    cylinder(double r, double h);

    // Accessors
    double getRadius();
    double getHeight();
    void setRadius(double r);
    void setHeight(double h);
    double area();
    double volume();
    void write(std::ostream& output);

private:
    double radius;
    double height;

};
#endif
main.cpp:

#include <iostream>
using namespace std;
#include <string>
#include "cylinder.h"
#include <iomanip>
#include "sphere.h"
#include "prism.h"
#include "cone.h"
#include "pyramid.h"
#include <cstdlib>



int main()
{
    double radius, height,sradius,length,width,rheight,cheight,cradius,pheight,plength;



    cout << "Enter cylinder height and radius >>> ";
    cin >> height >> radius;
    cylinder one (radius, height);
    cout << "The cylinder volume is " << setprecision(2)<<fixed<<one.volume () << endl;
    cout << "The cylinder surface area is " << setprecision(2)<<fixed<<one.area () << endl;
    cout <<"CYLINDER: "<<height<<", "<<radius<<endl;
}
#include <iostream>
using namespace std;
#include <string>
#include "cylinder.h"
#include <iomanip>
#include <cstdlib>

int main()
{
    double radius, height,sradius,length,width,rheight,cheight,cradius,pheight,plength;



    cout << "Enter cylinder height and radius >>> ";
    cin >> height >> radius;
    cylinder one (radius, height);
    cout << "The cylinder volume is " << setprecision(2)<<fixed<<one.volume () << endl;
    cout << "The cylinder surface area is " << setprecision(2)<<fixed<<one.area () << endl;
    cout <<"CYLINDER: "<<height<<", "<<radius<<endl;
}
#include <iostream>
#include <string>
#include <iomanip>

#include "cylinder.h"

using namespace std;

int main()
{
    double radius, height;



    cout << "Enter cylinder height and radius >>> ";
    cin >> height >> radius;
    cylinder one(radius, height);
    cout << "The cylinder volume is " << setprecision(2) << fixed << one.volume() << endl;
    cout << "The cylinder surface area is " << setprecision(2) << fixed << one.area() << endl;
    cout << "CYLINDER: " << height << ", " << radius << endl;
}
main.cpp:

#include <iostream>
using namespace std;
#include <string>
#include "cylinder.h"
#include <iomanip>
#include "sphere.h"
#include "prism.h"
#include "cone.h"
#include "pyramid.h"
#include <cstdlib>



int main()
{
    double radius, height,sradius,length,width,rheight,cheight,cradius,pheight,plength;



    cout << "Enter cylinder height and radius >>> ";
    cin >> height >> radius;
    cylinder one (radius, height);
    cout << "The cylinder volume is " << setprecision(2)<<fixed<<one.volume () << endl;
    cout << "The cylinder surface area is " << setprecision(2)<<fixed<<one.area () << endl;
    cout <<"CYLINDER: "<<height<<", "<<radius<<endl;
}
#include <iostream>
using namespace std;
#include <string>
#include "cylinder.h"
#include <iomanip>
#include <cstdlib>

int main()
{
    double radius, height,sradius,length,width,rheight,cheight,cradius,pheight,plength;



    cout << "Enter cylinder height and radius >>> ";
    cin >> height >> radius;
    cylinder one (radius, height);
    cout << "The cylinder volume is " << setprecision(2)<<fixed<<one.volume () << endl;
    cout << "The cylinder surface area is " << setprecision(2)<<fixed<<one.area () << endl;
    cout <<"CYLINDER: "<<height<<", "<<radius<<endl;
}
#include <iostream>
#include <string>
#include <iomanip>

#include "cylinder.h"

using namespace std;

int main()
{
    double radius, height;



    cout << "Enter cylinder height and radius >>> ";
    cin >> height >> radius;
    cylinder one(radius, height);
    cout << "The cylinder volume is " << setprecision(2) << fixed << one.volume() << endl;
    cout << "The cylinder surface area is " << setprecision(2) << fixed << one.area() << endl;
    cout << "CYLINDER: " << height << ", " << radius << endl;
}
#包括
#包括
#包括
#包括“汽缸.h”
使用名称空间std;
int main()
{
双半径,高度;
cout>高度>>半径;
气缸1(半径、高度);

您是否在main.cpp中包含了比cylider.h更多的文件。错误可能是由这些文件中的某些内容造成的。请发布一个。作为说明,当您执行
#ifndef-cylinder\u h
#define-cylinder\u h
时,
#endif
将在file@RSahu你说我在ma中包含更多文件是什么意思在cylinder中?我需要在cylinder.h中添加文件吗?您已经包括了sphere.h、pyramid.h等,但没有人知道这些文件的内容,这可能很重要。@Anisa,您正在
#包括
ing sphere.h、prism.h、cone.h和pyramid.h。从main.cpp中删除这些文件,看看问题是否仍然存在。cylinder仍然不在sc中操作。我快疯了。抱歉,但我必须否决这一项。不要将
使用命名空间std;
放在头文件中。在CPP文件中这样做是危险的,因为它会将所有std命名空间强制放在全局命名空间中,这会导致太多命名冲突和神秘bug无法列出。将其放在头文件中会通过静默转储来放大痛苦std为所有用户的头文件设置为全局。但我注意到网站中有一些代码。他们都将其放在头文件中。但我不认为使用名称空间std会影响“圆柱体不在范围内”但我注意到网站上有一些代码。它们都放在头文件中。这是一种不好的做法,但正如您所说,这可能不是问题所在。多个
使用名称空间std;
对一个没有任何重大影响,但确实不是@Anisa的问题。尽管如此,仍然很糟糕。请阅读:尝试从中删除所有include语句在识别任何参数之前,在实现文件中放入include和using语句;