Hello,
I tried posting this earlier but i do not think it went through because my internet connection was cut off. In case it did i am sorry for the repeating it, but have not seen it on the site.

Any ways. I am writing a class program and i have to use 3 seperate files. .h & .cpp & main program .cpp.

I am having problems with first .cpp i declared <iostream>, but it keeps saying cout is undeclared. Could someone please help? The cout is in the last function in .cpp file.

#ifndef CUBE_H
#define CUBE_H

class Cube
{
public:
    Cube();
    ~Cube();
    void setSide(double s);
    double getSide();
    double Area();
    double Volume();
    void print(ostream& os);
private:
    double Side;
};

#endif

#include <iostream>
#include "cube.h"

Cube::Cube()
{
}

Cube::~Cube()
{
}

void Cube::setSide(double s)
{
    Side = s <= 0 ? 1 : s;
}

double Cube::getSide()
{
    return Side;
}

double Cube::Area()
{
    return 6 * Side * Side;
}

double Cube::Volume()
{
    return Side * Side * Side;
}

void Cube::Properties(ostream& os)
{
    os << "Characteristics of this cube";
    os << "\nSide   = " << getSide();
    os << "\nArea   = " << Area();
    os << "\nVolume = " << Volume() << "\n\n";
}

#include "cube.h"

void main()
{
    Cube cube;
    cube.setSide(-12.55);
    cube.Properties();

    Cube de;
    de.setSide(28.15);
    de.Properties();
}

Recommended Answers

All 3 Replies

cout is part of the std namespace. You need to either prepend it with std:: (as in std::cout) or put using std::cout; in your file before you use it.

Now that I looked through your code, I'm surprised it compiles. You're not passing the necessary parameter to Properties in main(), and Properties isn't defined in the .h file. Plus, you don't even use cout anywhere.

Heya, new to c++, just finished a bit of c programming and ur reply really helped thanks

nobody told me about the std:: bit not even the book i was using ...but it works now cheers,.... by any chance could you advise when i ened the std:: bit in my codes as i said im a rookie and really am not sure why c++ needs that bit aded on

again thanks

Heya, new to c++, just finished a bit of c programming and ur reply really helped thanks

nobody told me about the std:: bit not even the book i was using ...but it works now cheers,.... by any chance could you advise when i ened the std:: bit in my codes as i said im a rookie and really am not sure why c++ needs that bit aded on

again thanks

Just put using namespace std; at the top of your files to make it simpler. You would use std:: in front of cout, cin, string and other things but those are the most common

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.