错误:此范围内未声明'cylinder'

时间:2015-08-13 17:20:39

标签: c++ xcode

我在Xcode中用C ++编写代码并接收:

  

错误:未在此范围内声明'cylinder'

头文件cylinder.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;

};

cylinder.cpp:

#include "cylinder.h"
double PI = 3.1415926535898;
#include "stdio.h"
using namespace std;

// Constructors
cylinder::cylinder()
{
    radius = 0;
    height = 0;
}
cylinder::cylinder(double r, double h)
{
    radius = r;
    height = h;
}

// Accessors
double cylinder::getRadius()
{
    return radius;
}
double cylinder::getHeight()
{
    return height;
}
// Setters
void cylinder::setRadius(double r)
{
    radius = r;
}
void cylinder::setHeight(double h)
{
    height = h;
}

// Calculations
double cylinder::area()
{
    return 2 * PI * radius * radius + 2 * PI * radius * height;
}
double cylinder::volume()
{
    return PI * radius * radius * height;
}

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

我被困了两天。我感到很困惑。我已经定义了圆柱类,我在网站上尝试了很多方法。有没有人可以帮助我? 它今晚到期了!

4 个答案:

答案 0 :(得分:1)

可悲的是一种试图找到答案而不是答案的方法。它已被发布作为答案,因为我不认为我可以在评论的范围内适应它。

我已删除了尚未提供的内容,更正了包含警卫的用法,改变了一些内容,并注释掉了不需要的内容。希望我已经对我做了什么以及为什么做了足够好的解释。如果没有,请问。

这个编译。我没有测试过逻辑。

如何处理它:

在main.cpp中有一堆包含但未提供的文件。为了获得工作基础,我已将它们评论出来。添加它们并逐个重建程序,直到程序停止编译。如果这不能使问题显而易见,那至少会缩小搜索范围。

修订了cylinder.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

修改了cylinder.cpp

#include "cylinder.h"
double PI = 3.1415926535898;
//#include "stdio.h" not used
//using namespace std; dangerous and not used.

// Constructors
cylinder::cylinder()
{
    radius = 0;
    height = 0;
}
cylinder::cylinder(double r, double h)
{
    radius = r;
    height = h;
}

// Accessors
double cylinder::getRadius()
{
    return radius;
}
double cylinder::getHeight()
{
    return height;
}
// Setters
void cylinder::setRadius(double r)
{
    radius = r;
}
void cylinder::setHeight(double h)
{
    height = h;
}

// Calculations
double cylinder::area()
{
    return 2 * PI * radius * radius + 2 * PI * radius * height;
}
double cylinder::volume()
{
    return PI * radius * radius * height;
}

修改了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;
}

答案 1 :(得分:0)

尝试将#endif放在头文件的末尾

答案 2 :(得分:-1)

我认为这是因为您使用“使用命名空间std”的倍数,在头文件中只使用一个,并且一定要把它放在#includes

之后

答案 3 :(得分:-1)

这是您在我的gnu / linux系统上编译和运行的表单中的原始代码。我试着不做很多改变。我认为,通过将旧代码与新代码进行比较,您可以看到最小的变化,这可能会为您解决一些问题。

在那之后,我展示了一个更清理的代码版本,再次我没有真正纠正它,特别是使用的样式和功能,但我只是试图削减很多不必要的东西。我认为实现不需要的东西也可以为你解决一些问题。

代码变化很小:

cylinder.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;

};

cylinder.cpp:

#include "cylinder.h"
double PI = 3.1415926535898;
#include "stdio.h"
using namespace std;

// Constructors
cylinder::cylinder()
{
    radius = 0;
    height = 0;
}
cylinder::cylinder(double r, double h)
{
    radius = r;
    height = h;
}

// Accessors
double cylinder::getRadius()
{
    return radius;
}
double cylinder::getHeight()
{
    return height;
}
// Setters
void cylinder::setRadius(double r)
{
    radius = r;
}
void cylinder::setHeight(double h)
{
    height = h;
}

// Calculations
double cylinder::area()
{
    return 2 * PI * radius * radius + 2 * PI * radius * height;
}
double cylinder::volume()
{
    return PI * radius * radius * height;
}

main.cpp中:

#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;
}

这是更清理的代码:

cylinder.h:

#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

cylinder.cpp:

#include "cylinder.h"

static const double PI = 3.1415926535898;

// Constructors
cylinder::cylinder() : radius(0.0), height(0.0)
{
}

cylinder::cylinder(double r, double h) : radius(r), height(h)
{
}

// Accessors
double cylinder::getRadius()
{
    return radius;
}

double cylinder::getHeight()
{
    return height;
}

// Setters
void cylinder::setRadius(double r)
{
    radius = r;
}

void cylinder::setHeight(double h)
{
    height = h;
}

// Calculations
double cylinder::area()
{
    return 2 * PI * radius * radius + 2 * PI * radius * height;
}

double cylinder::volume()
{
    return PI * radius * radius * height;
}

main.cpp中:

#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;
}