0

I wrote code in C++ in Xcode and receive:

error: ‘cylinder’ was not declared in this scope

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

I have been stuck for two days. I am so confused. I've already defined cylinder class, and I've tried many ways on the website. Is there anyone who can help me? It's due tonight!

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
Nancy
  • 11
  • 1
  • You are `#include`ing more files in main.cpp than just cylider.h. The error could result from something in those files. Please post an [MCVE](http://stackoverflow.com/help/mcve). – R Sahu Aug 13 '15 at 17:28
  • 2
    as a note, when you do `#ifndef cylinder_h` and `#define cylinder_h` the `#endif` goes at the end of the file – RyanP Aug 13 '15 at 17:29
  • @RSahu what do you mean I am including more files in main than cylinder?Do I need to add file in cylinder.h or somehing? – Nancy Aug 13 '15 at 17:37
  • You've included sphere.h, pyramid.h, etc. and no one knows the content of these files, it could be important. – tipaye Aug 13 '15 at 17:38
  • @Anisa, you are `#include`ing sphere.h, prism.h, cone.h and pyramid.h. Remove those from main.cpp and see if the problem is still there. – R Sahu Aug 13 '15 at 17:39
  • You need to figure out a lot of stuff, especially with the include guards, what should go in include files and including files in general, but by shifting the "using namespace std" to after all the include statements you should be able to compile and run. – tipaye Aug 13 '15 at 17:40
  • FWIW your code compiles and runs just fine in VS2015, minus the aforementined & unprovided headers – mark Aug 13 '15 at 17:40
  • @RSahu I removed other #includ in the main.cpp except cylinder but still didn't work. Still not in the scope. – Nancy Aug 13 '15 at 17:46
  • @tipaye do you mean add "using namespace std" after every #include in the main.cpp? – Nancy Aug 13 '15 at 17:47
  • Placing `using namespace std;` above a header causes that header to also use namespace std. This can have catastrophic results if the header was not expecting it. Do not use `using namespace std;` unless you know exactly what you are doing. And probably not even then. – user4581301 Aug 13 '15 at 19:23

4 Answers4

1

Sadly a method to try to find the answer and not the answer. It has been posted as an answer because I don't think I can fit it all in the bounds of a comment.

I have removed that which has not been provided, corrected the use if the include guard, shuffled a few things around, and commented out that which was not needed. Hopefully I have left a good enough explanation of what I did and why. If not, ask.

This compiles. I have not tested the logic.

What to do with it:

In main.cpp there are a bunch of files that were included but not provided. To get a working base, I have commented them out. Add them and rebuild the program one by one until the program stops compiling. If this does not make the problem obvious, it has at least reduced the search area.

Revised 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

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

Revised 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;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
0

try to put #endif at the end of the header file

Sandra Sukarieh
  • 133
  • 1
  • 16
  • 1
    try removing all include statements from header file, and in the implementation file put include and using statements before identifying any parameter. also define PI like this: const double pi = 3.1415926; – Sandra Sukarieh Aug 13 '15 at 18:02
-1

I think it's because of your multiples "using namespace std" , use only one , that one in the header file , and be sure to put it after all #includes

  • cylinder still not in the scope. I am going crazy. – Nancy Aug 13 '15 at 17:59
  • 1
    Sorry, but I have to downvote this one. Do not put `using namespace std;` in a header. It is dangerous to do in a CPP file as it forces all the std namespace into the global namespace and that causes too many naming collisions and mystery bugs to list. Putting it into a header magnifies the pain by silently dumping std into global for all users of the header. – user4581301 Aug 13 '15 at 18:00
  • But I noticed some code in the website. They all put it in the header file. But I don't think using namespace std affects "cylinder is not in the scope" – Nancy Aug 13 '15 at 18:02
  • ***But I noticed some code in the website. They all put it in the header file.*** This is a bad practice but as you said this is likely not the problem. – drescherjm Aug 13 '15 at 18:05
  • Multiple `using namespace std;` doesn't have any significant impact over one, but indeed not @Anisa 's problem. Still bad though. Give this a read: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – user4581301 Aug 13 '15 at 18:17
-1

Here's your original code in a form that compiles and runs on my gnu/linux system. I tried to not to make many changes. I think you'll benefit from comparing your old code with new code to see what the minimal changes are, this may clear up some things for you.

After that, I show a more cleaned up version of the code, again I haven't really corrected it, especially the style and features used, but I've just tried to cut away a lot of the unnecessary things. I think realising what isn't neeed may also clear up some issues for you.

Code with minimal changes:

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

Here's the code with a bit more cleanup:

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;
}
tipaye
  • 454
  • 3
  • 6