Hello, i've been learning java for a few months now, but i woulld also like to try c++. I tried rewriting one of my exercises in c++, but i cant seem to get objects working.
These lines all prodduce errors.
Cylinder outer(5,5);
Cylinder inner(4,4);
cout << "\n" << outer.volume() << inner.volume() << "\n";

main.cpp:19: undefined reference to `Cylinder::Cylinder(double, double)'
main.cpp:20: undefined reference to `Cylinder::Cylinder(double, double)'
main.cpp:21: undefined reference to `Cylinder::volume()'
main.cpp:21: undefined reference to `Cylinder::volume()'

Main.cpp

#include <stdlib.h>
#include <iostream>
#include <math.h>
#include "Cylinder.h"
using namespace std;





int main(int argc, char** argv) {
    Cylinder outer(5,5);
    Cylinder inner(4,4);
    cout  << "\n" << outer.volume() << inner.volume() << "\n";
    return (EXIT_SUCCESS);
}

Cylinder.cpp

#include <iostream>
#include <math.h>
using namespace std;

class Cylinder{

private:

    double radius;
    double height;

public:
    Cylinder()
    {
        radius = 0;
        height = 0;
    }

    Cylinder(double aradius, double aheight)
    {
        radius = aradius;
        height = aheight;
    }

    void set_radius(double aradius)
    {radius = aradius;}

    double get_radius()
    {return radius;}

    void set_height(double aheight)
    {height = aheight;}

    double get_height()
    {return height;}

    void set_both(double aradius, double aheight)
    {
        radius = aradius;
        height = aheight;
    }

    void print()
    {cout << "The radius is " << radius << "\nThe height is "
            << height << ".\n";}

    double volume()
    {
        const double PI = 3.141592;
        return (PI*pow(radius,2)*height);
    }

    double surface()
    {
        const double PI = 3.141592;
        return ((2*PI*radius*height)+(2*PI*pow(radius,2)));
    }

    //true if the cylinder in this object is greater.
    //1 greater, 0 equal, -1 smaller

    short compare(double volume2)
    {
        if(this->volume()>volume2)
        return 1;
        if(this->volume()==volume2)
        return 0;
        else
        return -1;
    }

    Cylinder duplicate()
    {
        return *this;
    }

     virtual ~Cylinder() {}
};

Cylinder.h

#ifndef CYLINDER_H
#define	CYLINDER_H

class Cylinder {
private:
    double radius;
    double height;

public:
    Cylinder();
    Cylinder(double aradius, double aheight);
    void set_radius(double aradius);
    double get_radius();
    void set_height(double aheight);
    double get_height();
    void set_both(double aradius, double aheight);
    void print();
    double volume();
    double surface();
    short compare(double volume2);
    Cylinder duplicate();
    virtual ~Cylinder(){};

};
#endif	/* _CYLINDER_H */
Fbody commented: Good problem description and thanks for trying to use code tags. Keep it up. +1

<deleted> Nevermind. Sorry.

See VVVVVVVVVVVVVVVV

Thanks for trying to use [code=syntax] ... code tags ... [/code]. They didn't work because your closing tag is incorrect. Take out the "=syntax" part and they should work.

You have duplicated your class declaration. When you define a member function in a source file (a *.cpp file) that is separate from the header file (a *.h file) you do not re-use the "class" keyword. Instead you use the class name and the "scope resolution operator" ( '::' ).

Example:

//class.h
class myClass {
  private:
    int prvInt;
  public:
    myClass();  //default constructor
    myClass(int);  //specified constructor
};
//class.cpp
#include "class.h"

myClass::myClass() {  //default constructor
  prvInt = 0;
}

myClass::myClass(int newPrvInt) {  //specified constructor
  prvInt = newPrvInt;
}

Your main would be as normal.

Thanks, this seems much simpler in java.

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.