mikeym
March 30th, 2008, 07:02 PM
Hi,
I'm trying to get started with C++ and I'm finding it about as clear as mud. There appear to be a lot of functions that work behind the scenes that you just have to know how they work.
I'm coming from a background of java and scipting languages such as PHP and bash and (*cough*) VB. So that's the language I'll employ to describe what I'm looking for.
I want to create an abstract class Shape and a non abstract implementation of it Sphere. I wanted to put most of the standard functions into Shape and have a abstract function Create to do the business of actually creating the specific shapes.
Now no matter what I've tried I can't get it to compile. I've even tried abandoning the abstract class idea because I thought it might be simpler but that's not worked.
Anyway this is where I'm at:
Shape.h
/***
Author: Mikey
Email: abc.mikey@googlemail.com
Modified: 30/03/08
***/
#ifndef SHAPE_H
#define SHAPE_H 1
#include <GL/gl.h>
#include "svl/SVL.h"
#include "svl/SVLgl.h"
#define PI 3.1415926535897932384626433832795
class Shape {
public:
Shape() {
maxLong=0;
maxLat=0;
closedlong=false;
closedlat=false;
}
void setLong(int Long);
void setLat(int Lat);
int getLong() { return maxLong+1; }
int getLat() { return maxLat +1; }
bool ClosedLong() { return closedlong; }
bool ClosedLat() { return closedlat; }
Vec3 getMatrix(int Long, int Lat);
void setMatrix(int Long, int Lat, Vec3 v);
void Create();
private:
Vec3 *vmatrix;
int maxLong;
int maxLat;
bool closedlong;
bool closedlat;
};
class Sphere : public Shape {
public:
void Create();
void Create(float rad);
Sphere(float rad) { Create(rad); }
Sphere() { }
int getSegments() { return segments; }
void setSegments(int segs);
private:
float rad;
int segments;
};
#endif
Shape.cpp
/***
Author: Mikey
Email: abc.mikey@googlemail.com
Modified: 30/03/08
***/
#include "Shape.h"
void Shape::setLong(int Long) {
maxLong = Long + 1;
if(maxLong >= 0 && maxLat >= 0) {
delete [] vmatrix;
vmatrix = new Vec3[maxLong][maxLat];
}
}
void Shape::setLat(int Lat) {
maxLat = Lat + 1;
if(maxLong >= 0 && maxLat >= 0) {
delete [] vmatrix;
vmatrix = new Vec3[maxLong][maxLat];
}
}
Vec3 Shape::getMatrix(int Long, int Lat) {
if (Long < 0 || Long > maxLong || Lat < 0 || Lat > maxLat ) {
std::cerr << "Shape::getMatrix - ERROR **: Out Of Range." << std::endl;
}
return *vmatrix[Long][Lat];
}
void Shape::setMatrix(int Long, int Lat, Vec3 v) {
if (Long < 0 || Long > maxLong || Lat < 0 || Lat > maxLat ) {
std::cerr << "Shape::setMatrix - ERROR **: Out Of Range." << std::endl;
}
*vmatrix[Long][Lat]=v;
}
void Shape::Create() {
}
void Sphere::Create() {
rad = 1;
setSegments(20);
Create(rad);
}
void Sphere::Create(float rad) {
float theta, phi;
for(int i=0;i<=maxLat;i++) {
theta = i*PI/maxLat;
for(int j=0;j<=maxLong;j++) {
phi = j*2*PI/maxLong;
x = r*cos(theta)*sin(phi);
y = r*sin(theta)*sin(phi);
z = r*cos(phi);
vmatrix[j][i]= Vec3(x,y,z);
printf("V%i%i(%f,%f,%f) at %f by %f\n", j, i, x, y, z, 180*theta/PI, 180*phi/PI);
}
}
}
void Sphere::setSegments(int segs) {
if (segs < 2) {
std::cerr << "Shape::setSegments - ERROR **: Segments must be >= 2." << std::endl;
}
if (segs % 2 != 0) {
std::cerr << "Shape::setSegments - ERROR **: Must be even number of segments." << std::endl;
}
setLat(segs/2+1):
setLong(segs);
segments = segs;
}
testshapes.cpp
#include <stdio.h>
#include <stdlib.h>
#include "Shape.h"
// Init everything
int main(int argc, char* argv[]) {
Sphere s(1);
return (0);
};
Oh yeh! Here's the current error (they all centre around the constructors and create):
/tmp/ccJduq65.o: In function `Sphere::Sphere(float)':
testshapes.cpp:(.text._ZN6SphereC1Ef[Sphere::Sphere(float)]+0x2a): undefined reference to `Sphere::Create(float)'
collect2: ld returned 1 exit status
I'm trying to get started with C++ and I'm finding it about as clear as mud. There appear to be a lot of functions that work behind the scenes that you just have to know how they work.
I'm coming from a background of java and scipting languages such as PHP and bash and (*cough*) VB. So that's the language I'll employ to describe what I'm looking for.
I want to create an abstract class Shape and a non abstract implementation of it Sphere. I wanted to put most of the standard functions into Shape and have a abstract function Create to do the business of actually creating the specific shapes.
Now no matter what I've tried I can't get it to compile. I've even tried abandoning the abstract class idea because I thought it might be simpler but that's not worked.
Anyway this is where I'm at:
Shape.h
/***
Author: Mikey
Email: abc.mikey@googlemail.com
Modified: 30/03/08
***/
#ifndef SHAPE_H
#define SHAPE_H 1
#include <GL/gl.h>
#include "svl/SVL.h"
#include "svl/SVLgl.h"
#define PI 3.1415926535897932384626433832795
class Shape {
public:
Shape() {
maxLong=0;
maxLat=0;
closedlong=false;
closedlat=false;
}
void setLong(int Long);
void setLat(int Lat);
int getLong() { return maxLong+1; }
int getLat() { return maxLat +1; }
bool ClosedLong() { return closedlong; }
bool ClosedLat() { return closedlat; }
Vec3 getMatrix(int Long, int Lat);
void setMatrix(int Long, int Lat, Vec3 v);
void Create();
private:
Vec3 *vmatrix;
int maxLong;
int maxLat;
bool closedlong;
bool closedlat;
};
class Sphere : public Shape {
public:
void Create();
void Create(float rad);
Sphere(float rad) { Create(rad); }
Sphere() { }
int getSegments() { return segments; }
void setSegments(int segs);
private:
float rad;
int segments;
};
#endif
Shape.cpp
/***
Author: Mikey
Email: abc.mikey@googlemail.com
Modified: 30/03/08
***/
#include "Shape.h"
void Shape::setLong(int Long) {
maxLong = Long + 1;
if(maxLong >= 0 && maxLat >= 0) {
delete [] vmatrix;
vmatrix = new Vec3[maxLong][maxLat];
}
}
void Shape::setLat(int Lat) {
maxLat = Lat + 1;
if(maxLong >= 0 && maxLat >= 0) {
delete [] vmatrix;
vmatrix = new Vec3[maxLong][maxLat];
}
}
Vec3 Shape::getMatrix(int Long, int Lat) {
if (Long < 0 || Long > maxLong || Lat < 0 || Lat > maxLat ) {
std::cerr << "Shape::getMatrix - ERROR **: Out Of Range." << std::endl;
}
return *vmatrix[Long][Lat];
}
void Shape::setMatrix(int Long, int Lat, Vec3 v) {
if (Long < 0 || Long > maxLong || Lat < 0 || Lat > maxLat ) {
std::cerr << "Shape::setMatrix - ERROR **: Out Of Range." << std::endl;
}
*vmatrix[Long][Lat]=v;
}
void Shape::Create() {
}
void Sphere::Create() {
rad = 1;
setSegments(20);
Create(rad);
}
void Sphere::Create(float rad) {
float theta, phi;
for(int i=0;i<=maxLat;i++) {
theta = i*PI/maxLat;
for(int j=0;j<=maxLong;j++) {
phi = j*2*PI/maxLong;
x = r*cos(theta)*sin(phi);
y = r*sin(theta)*sin(phi);
z = r*cos(phi);
vmatrix[j][i]= Vec3(x,y,z);
printf("V%i%i(%f,%f,%f) at %f by %f\n", j, i, x, y, z, 180*theta/PI, 180*phi/PI);
}
}
}
void Sphere::setSegments(int segs) {
if (segs < 2) {
std::cerr << "Shape::setSegments - ERROR **: Segments must be >= 2." << std::endl;
}
if (segs % 2 != 0) {
std::cerr << "Shape::setSegments - ERROR **: Must be even number of segments." << std::endl;
}
setLat(segs/2+1):
setLong(segs);
segments = segs;
}
testshapes.cpp
#include <stdio.h>
#include <stdlib.h>
#include "Shape.h"
// Init everything
int main(int argc, char* argv[]) {
Sphere s(1);
return (0);
};
Oh yeh! Here's the current error (they all centre around the constructors and create):
/tmp/ccJduq65.o: In function `Sphere::Sphere(float)':
testshapes.cpp:(.text._ZN6SphereC1Ef[Sphere::Sphere(float)]+0x2a): undefined reference to `Sphere::Create(float)'
collect2: ld returned 1 exit status